-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMAPtxtRead.m
143 lines (123 loc) · 4.83 KB
/
CMAPtxtRead.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function Data = CMAPtxtRead(filename)
%% Import data from text file.
% Script for importing data from the following text file:
%
% D:\CMAP\20161121\20mAT5T55Nervridge1Sel.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2016/12/01 11:07:01
%% Initialize variables.
% filename = 'D:\CMAP\20161121\20mAT5T55Nervridge2.txt';
% filename = 'D:\CMAP\20161205\20mA82-92NervridgeASC2P1.txt';
delimiter = '\t';
%% Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%q%q%q%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric text to numbers.
% Replace non-numeric text with NaN.
raw = repmat({''},length(dataArray{1}),2);
for col=1:2
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),2);
for col=2
% Converts text in the input cell array to numbers. Replaced non-numeric
% text with NaN.
rawData = dataArray{col};
fprintf('\nProgress:\n')
stringout=[num2str(1./size(rawData, 1)),'%%'];
fprintf(stringout)
h = msgbox('Reading data. Please wait for a moment...');
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
removeout=repmat('\b',1,length(stringout)-1);
stringout=[num2str(round((row./...
size(rawData, 1))*100)),'%% '];
fprintf([removeout stringout])
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',')
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
close(h)
fprintf('\nDone!\n')
end
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
Data = cell2mat(raw);
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me R;
% %%
% Time = Data(9:808,1)*1000;
%
% StartTrial = 48;
% EndTrial = StartTrial + 9;
%
% for i = 1:10
% Rec(i).RecData = Data(9+808*(StartTrial-2+i):808*(StartTrial+i-1),2)*0.001;
% end
%
% MeanData = Rec(1).RecData;
% for i = 2:10
% MeanData = MeanData + Rec(i).RecData;
% end
% MeanData = MeanData./10;
% figure,
% for i = 1:10
% plot(Time,-Rec(i).RecData)
% hold on
% end
%
% Loc = 1:800;
% Loc = Loc';
% figure, plot(Time,-MeanData)
% MeanData_withoutStim = MeanData;
% MeanData_withoutStim(1:100) = 0;
% [PeakAmplitude,Peakloc] = findpeaks(-MeanData_withoutStim,'MinPeakHeight', max(-MeanData)-10/1000);
% MeanDataShorterPeakLoc = -MeanData(Loc<Peakloc);
% TimeShorterPeakLoc = Loc(Loc<Peakloc);
% LocNegative1 = max(Loc(MeanDataShorterPeakLoc < 0));
% TimeZero1 = Time(LocNegative1)+0.025*abs(MeanData(LocNegative1))/(abs(MeanData(LocNegative1))+MeanData(LocNegative1+1));
%
% LocNegative2 = min(Loc(-MeanData<0&Loc>Loc(Peakloc)));
% TimeZero2 = Time(LocNegative2-1)+0.025*(-MeanData(LocNegative2-1))/(abs(MeanData(LocNegative2-1))+abs(MeanData(LocNegative2)));
%
% Latency = TimeZero1-1;
%
% disp(['PeakAmplitude is ' num2str(PeakAmplitude) 'mV']);
% disp(['Latency is ' num2str(Latency) 'ms']);
% disp(['Duration is ' num2str(TimeZero2-TimeZero1) 'ms']);
% hold on
% plot(TimeZero1, 0, 'o')
% plot(TimeZero2, 0, 'o')
% plot(Time(Peakloc), PeakAmplitude, '^')