-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplotSPMnod.m
265 lines (191 loc) · 6.27 KB
/
plotSPMnod.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
function plotSPMnod( Names , Onsets , Durations )
%PLOTSPMNOD plots Names Onsets and Durations (n.o.d.) defined for SPM
%
% SYNTAX
% (1) plotSPMnod( Names , Onsets , Durations )
% (2) plotSPMnod
%
% INPUTS
% 1. Names : defined for SPM
% 2. Onsets : defined for SPM
% 3. Durations : defined for SPM
%
% NOTES
% (1) All inputs are cells used for SPM design matrix
% (2) Try to use 'names', 'onsets', 'durations' from base workspace
%
% See also spm
% CENIR-ICM , 2015
%% Check input arguments
Arguments = {...
'Names' ;...
'Onsets' ;...
'Durations' ...
};
nb_Args = length(Arguments);
% Must be 3 input arguments, or try with the base workspace
if nargin > 0
% narginchk(nb_Args,nb_Args)
% narginchk introduced in R2011b
if nargin ~= nb_Args
error('%s uses %d input argument(s)',mfilename,nb_Args)
end
else
% Import variables from base workspace
vars = evalin('base','whos');
% Check each variable from base workspace
for v = 1 : length(vars)
% names ?
if strcmp ( vars(v).name , 'names' ) && strcmp ( vars(v).class , 'cell' )
Names = evalin('base','names');
end
% onsets ?
if strcmp ( vars(v).name , 'onsets' ) && strcmp ( vars(v).class , 'cell' )
Onsets = evalin('base','onsets');
end
% durations ?
if strcmp ( vars(v).name , 'durations' ) && strcmp ( vars(v).class , 'cell' )
Durations = evalin('base','durations');
end
end
% Check if all vairables have been found in the base workspace
if ~ ( exist('Names','var') && exist('Onsets','var') && exist('Durations','var') )
error('Even without input arguments, the function tries to use the base workspace variables, but failed.')
end
end
% Correct arguments ?
Length = nan(size(Arguments));
for arg = 1 : nb_Args
validateattributes(eval(Arguments{arg}),{'cell'},{'vector'},mfilename,Arguments{arg},arg)
Length(arg) = length(eval(Arguments{arg}));
end
% All inputs with the same dimension ?
collinear = cross( Length , ones(size(Arguments)) );
if any(collinear) % is Length vector collinear to [1 ; 1 ; 1] ?
error('All inputs must have the same dimensions')
end
% Names is only strings ?
if ~iscellstr(Names)
error('Names must be a cell array of strings')
end
for arg_ = 1 : Length(1)
% Onset and Durations have same dimensions inside ?
if length(Onsets{arg_}) ~= length(Durations{arg_})
error( 'Onsets{%d} and Durations{%d} have different dimensions : %d and %d ' , arg_ , arg_ , length(Onsets{arg_}) , length(Durations{arg_}) )
end
% Onsets numeric inside ?
if ~isnumeric(Onsets{arg_})
error( 'Onsets{%d} must be numeric ', arg_ )
end
% Durations numeric inside ?
if ~isnumeric(Durations{arg_})
error( 'Durations{%d} must be numeric ', arg_ )
end
end
% Change each column vector into row, just for convenience
for arg = 1 : nb_Args
if isrow(eval(Arguments{arg}))
eval([Arguments{arg} '=' Arguments{arg} ''';'])
end
end
for o = 1 : length(Onsets)
if isrow(Onsets{o})
Onsets{o} = Onsets{o}';
end
end
for d = 1 : length(Durations)
if isrow(Durations{d})
Durations{d} = Durations{d}';
end
end
%% Prepare curves
% Pre-allocate
Curves = cell(length(Names),1);
% For condition
for c = 1 : size( Curves , 1 )
% Catch data for this condition
data = [ Onsets{c} Durations{c} ones(size(Onsets{c},1),1) ];
% Number trials in the condition
N = size( data , 1 );
% Here we need to build a curve that looks like recangles
for n = N:-1:1
switch n
case N
% Split data above & under the point
dataABOVE = data( 1:n-1 ,: );
dataMIDDLE = data( n ,: );
dataUNDER = NaN( 1 , size(data,2) );
case 1
% Split data above & under the point
dataABOVE = data( 1:n-1 ,: );
dataMIDDLE = data( n ,: );
dataUNDER = data( n+1:end , : );
otherwise
% Split data above & under the point
dataABOVE = data( 1:n-1 ,: );
dataMIDDLE = data( n ,: );
dataUNDER = data( n+1:end , : );
end
% Add a point ine curve to build a rectangle
data = [ ...
dataABOVE ;...
% Add points to create a rectangle
dataMIDDLE(1,1) NaN NaN ;...
dataMIDDLE(1,1) NaN 0 ;...
dataMIDDLE(1,:) ;...
dataMIDDLE(1,1)+dataMIDDLE(1,2) NaN 1 ;...
dataMIDDLE(1,1)+dataMIDDLE(1,2) NaN 0 ;...
dataUNDER ...
] ;
end
% Delete second column
if ~isempty(data)
data(:,2) = [];
end
% Store curves
Curves{c} = data;
end
%% Plot
% Figure
figure( ...
'Name' , mfilename , ...
'NumberTitle' , 'off' ...
)
hold all
% For each Event, plot the curve
for c = 1 : size( Curves , 1 )
if ~isempty(Curves{c})
plot( Curves{c}(:,1) , Curves{c}(:,2) + c )
else
plot(0,NaN)
end
end
% Legend
lgd = legend( Names(:) );
set(lgd,'Interpreter','none')
%% Adjust window display
% Change the limit of the graph so we can clearly see the rectangles.
scale = 1.1;
old_xlim = xlim;
range_x = old_xlim(2) - old_xlim(1);
center_x = mean( old_xlim );
new_xlim = [ (center_x - range_x*scale/2 ) center_x + range_x*scale/2 ];
old_ylim = ylim;
range_y = old_ylim(2) - old_ylim(1);
center_y = mean( old_ylim );
new_ylim = [ ( center_y - range_y*scale/2 ) center_y + range_y*scale/2 ];
% Set new limits
xlim( new_xlim )
ylim( new_ylim )
% ================ Change YTick and YTickLabel ================
% Put 1 tick in the middle of each event
set( gca , 'YTick' , (1:size( Names , 1 ))+0.5 )
% Set the tick label to the event name
set( gca , 'YTickLabel' , Names )
% Not all versions of MATLAB have this option
try
set(gca, 'TickLabelInterpreter', 'none')
catch %#ok<CTCH>
end
end