-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui_callback.m
352 lines (308 loc) · 16.1 KB
/
gui_callback.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function gui_callback(action)
% This function is called by EEG_TF
% This file contains the callbacks to the main window
% Handles to other controls in this main window
global ht_loadStatus
global ht_weightsStatus
global ht_channelsStatus
global hcm_electrode_space
global ht_samplesStatus
global ht_timerangeStatus
global ht_samplingrateStatus
global ht_eventsStatus
global ht_errorStatus
global hpm_individual
global hpm_compare
global he_chanOI
global he_freqOI
global he_timeOI
global hpm_method
global hcm_dB
global he_baseline
global he_timeLM
global he_freqLM
% Global variables to store all the values
global g_EEG_dataset;
global g_params;
global c_Individual_strV;
global c_Compare_strV;
global c_Method_strV;
global c_binary_strV;
c_Individual_strV = {'individual'; 'group'};
c_Compare_strV = {'compare'; 'merge'};
c_Method_strV = {'stft'; 'wavelets'};
c_binary_strV = {'no'; 'yes'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
watchonInFigure = watchon;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch action
%-------------------------------------
case 'InitAll'
% if the data is initiated
if ~isempty(g_EEG_dataset)
% if EEGlab set is loaded
if ~isempty(g_EEG_dataset.EEG)
set(ht_loadStatus, 'String', '');
g_EEG_dataset.data = [];
if iscell(g_EEG_dataset.filenames)
set(ht_loadStatus, 'String', strcat(num2str(length(g_EEG_dataset.filenames)),' dataset(s)'));
else
set(ht_loadStatus, 'String', '1 dataset(s)');
end
set(ht_channelsStatus, 'String', num2str(g_EEG_dataset.EEG.nbchan));
set(ht_samplesStatus, 'String', num2str(g_EEG_dataset.EEG.pnts));
set(ht_timerangeStatus, 'String', strcat('[',num2str(round(g_EEG_dataset.EEG.xmin*1000)),...
':', num2str(round(g_EEG_dataset.EEG.xmax*1000)), ']ms'));
set(ht_samplingrateStatus, 'String', num2str(g_EEG_dataset.EEG.srate));
% set timeOI and freqOI according the data
set(he_freqOI, 'String', strcat('1:', num2str(round(g_EEG_dataset.EEG.srate/2))));
g_params.freqOI = [1:round(g_EEG_dataset.EEG.srate/2)];
set(he_timeOI, 'String', strcat(num2str(round(g_EEG_dataset.EEG.xmin*1000)),...
':', num2str(round(g_EEG_dataset.EEG.xmax*1000))));
g_params.timeOI = [round(g_EEG_dataset.EEG.xmin*1000),round(g_EEG_dataset.EEG.xmax*1000)];
g_params.timeOI = [g_EEG_dataset.EEG.xmin*1000, g_EEG_dataset.EEG.xmax*1000];
if ~isempty(g_EEG_dataset.EEG.icaweights)
set(ht_weightsStatus, 'String', 'Included');
g_EEG_dataset.w = g_EEG_dataset.EEG.icaweights;
else
set(ht_weightsStatus, 'String', 'Not included');
g_EEG_dataset.w = ones(g_EEG_dataset.EEG.nbchan, g_EEG_dataset.EEG.nbchan);
end
g_params.baseline = [g_EEG_dataset.EEG.xmin*1000, g_EEG_dataset.EEG.xmax*1000];
if g_EEG_dataset.EEG.xmin < 0
g_params.baseline(2) = 0;
end
set(he_baseline, 'String', strcat(num2str(round(g_params.baseline(1))),...
':', num2str(round(g_params.baseline(2)))));
else
% if data is passed in the function from the workspace
if ~isempty(g_EEG_dataset.data)
data_sz = size(g_EEG_dataset.data);
set(ht_loadStatus, 'String', 'Loaded');
set(ht_channelsStatus, 'String', num2str(data_sz(1)));
set(ht_samplesStatus, 'String', num2str(data_sz(2)));
set(ht_timerangeStatus, 'String', ...
sprintf('[%i:%i]ms',round(g_EEG_dataset.time(1)*1000), round(g_EEG_dataset.time(end)*1000)));
set(ht_samplingrateStatus, 'String', num2str(g_EEG_dataset.fs));
% set timeOI and freqOI according the data
set(he_freqOI, 'String', strcat('1:', num2str(round(g_EEG_dataset.fs/2))));
g_params.freqOI = [1:round(g_EEG_dataset.fs/2)];
set(he_timeOI, 'String', strcat(num2str(round(g_EEG_dataset.time(1)*1000)),...
':', num2str(round(g_EEG_dataset.time(2)*1000))));
g_params.timeOI = [g_EEG_dataset.time(1)*1000, g_EEG_dataset.time(2)*1000];
% set baseline - all the timeperiod by default, if
% there is prestimulus period - take it
g_params.baseline = [g_EEG_dataset.time(1)*1000, g_EEG_dataset.time(2)*1000];
if g_EEG_dataset.time(1) < 0
g_params.baseline(2) = 0;
end
set(he_baseline, 'String', strcat(num2str(round(g_params.baseline(1))),...
':', num2str(round(g_params.baseline(2)))));
% disable the chose of conditions, merge and individual
set(hpm_compare,'Enable','off');
set(hpm_individual,'Enable','off');
end
end
% initiate the settings with string values in the workspace
gui_callback ChangeIndividual
gui_callback ChangeCompare
gui_callback ChangeMethod
gui_callback ChangedB
set(ht_errorStatus, 'String', 'Ready', 'ForegroundColor', [1 1 1]);
end
%---------------------------------------
case 'LoadData'
% in the beginning, we do not load all the data at the same time -
% we 'preload' the first dataset to get info about events, the rest
% of the data is uploaded later
[g_EEG_dataset.filenames, g_EEG_dataset.pathname] = uigetfile('*.set', 'Select the individual or group .set files.','MultiSelect','on');
% uigetfile() returns cell if there is multiselect and simple char if there is only one name selected
if iscell(g_EEG_dataset.filenames)
g_EEG_dataset.nb_files = size(g_EEG_dataset.filenames, 2);
first_filename = g_EEG_dataset.filenames{1, 1};
else
g_EEG_dataset.nb_files = 1;
first_filename = g_EEG_dataset.filenames;
end
% try to read the first file, check if EEGlab is in the path, addpath if not
try
g_EEG_dataset.EEG = pop_loadset(strcat(g_EEG_dataset.pathname,'\',first_filename));
catch ME
if (strcmp(ME.identifier,'MATLAB:UndefinedFunction'))
warning('EEGLAB is not in the path');
set(ht_errorStatus, 'String', 'EEGLAB is not in the path', 'ForegroundColor', [1 0 0]);
selpath = uigetdir(path,'Select the path to EEGLAB library');
addpath(genpath(selpath));
g_EEG_dataset.EEG = pop_loadset(strcat(g_EEG_dataset.pathname,'\',first_filename));
end
end
% cast the data in double precision
g_EEG_dataset.EEG.data = double(g_EEG_dataset.EEG.data);
g_EEG_dataset.time = [g_EEG_dataset.EEG.xmin, g_EEG_dataset.EEG.xmax];
g_EEG_dataset.fs = g_EEG_dataset.EEG.srate;
for event = 1:length(g_EEG_dataset.EEG.event)
all_events{event} = g_EEG_dataset.EEG.event(event).type;
end
all_events = unique(all_events);
g_EEG_dataset.events = all_events;
clear all_events
% enable all the settings
set(hpm_compare,'Enable','on');
set(hpm_individual,'Enable','on');
gui_callback InitAll;
gui_callback_advanced InitAll;
case 'LoadWeights'
[weight_filenames, weight_pathname] = uigetfile('*.mat', 'Select the (g)BSS w weights .mat file.');
q = load(strcat(weight_pathname,'\',weight_filenames));
fields = fieldnames(q);
name = strcat('q.',fields{1});
% name = strcat('q.',weight_filenames(1:end-4));
g_EEG_dataset.w = eval(name);
set(ht_weightsStatus, 'String', 'Loaded');
set(ht_errorStatus, 'String', 'Ready', 'ForegroundColor', [1 1 1]);
case 'KeepElectrodes' % initially it is off
if isempty(g_EEG_dataset) % if there is no EEG dataset it won't work
set(ht_errorStatus, 'String', 'Please, load the data first!', 'ForegroundColor', [1 0 0]);
set(hcm_electrode_space, 'Value', 0);
elseif get(hcm_electrode_space, 'Value') == 1 % keep electrode space
g_EEG_dataset.w = ones(g_EEG_dataset.EEG.nbchan);
set(ht_errorStatus, 'String', 'Ready', 'ForegroundColor', [1 1 1]);
elseif (get(hcm_electrode_space, 'Value') == 0) && ~isempty(g_EEG_dataset.EEG.icaweights)
% if the setting was off, then on, then off again, use the
% included weights
g_EEG_dataset.w = g_EEG_dataset.EEG.icaweights;
set(ht_weightsStatus, 'String', 'Included');
set(ht_errorStatus, 'String', 'Load the (g)BSS weigths if you do not want use the uncluded ones', 'ForegroundColor', [1 1 1]);
else
set(ht_errorStatus, 'String', 'Load the (g)BSS weigths as there are no uncluded ones!', 'ForegroundColor', [1 0 0]);
end
case 'ChooseEvents'
% if some events were chosen before, remove them the params
if ~isempty(g_params.eventsOI)
g_params.eventsOI = [];
end
% events can be chosen only after some data is loaded, otherwise
% there is no list of events available
if isempty(g_EEG_dataset)
set(ht_errorStatus, 'String', 'Please, load the data first!', 'ForegroundColor', [1 0 0]);
elseif isempty(g_EEG_dataset.events)
% this is the case for the data from workspace
set(ht_errorStatus, 'String', 'There is no information about events in this dataset!', 'ForegroundColor', [1 0 0]);
else
indx = listdlg('PromptString','Include condition(s)','ListString', g_EEG_dataset.events);
eventsOI = '';
if length(indx) == length(g_EEG_dataset.events)
g_params.eventsOI = g_EEG_dataset.events;
eventsOI = 'all';
else
for num_indx = 1:length(indx)
g_params.eventsOI{1,num_indx} = g_EEG_dataset.events{indx(num_indx)};
eventsOI = strcat(eventsOI, g_params.eventsOI{1,num_indx}, '; ');
end
end
clear indx
set(ht_eventsStatus, 'String', eventsOI);
end
gui_callback ChangeCompare
case 'ChangeCompare' % compare = 1, merge = 2
g_params.compare = c_Compare_strV{get(hpm_compare, 'Value')};
% there has to be 2 events for comparison
if size(g_params.eventsOI,2) ~= 2 && strcmp(g_params.compare, 'compare')
g_params.compare = c_Compare_strV{2};
end
case 'ChangeIndividual' % individual = 1, group = 2
g_params.individual = c_Individual_strV{get(hpm_individual, 'Value')};
% if there is only one file, only individual amalysis is possible
if g_EEG_dataset.nb_files == 1
g_params.individual = c_Individual_strV{1};
end
case 'ChangeChanOI'
% in case they are written in the form of '1,2,3'
g_params.chanOI = str2double(strsplit(get(he_chanOI, 'String'), ','));
%in case they are written in the form '1:5'
if isnan(g_params.chanOI)
lims = str2double(strsplit(get(he_chanOI, 'String'),':'));
g_params.chanOI = lims(1):lims(2);
end
case 'ChangeFreqOI'
lims = str2double(strsplit(get(he_freqOI, 'String'),':'));
g_params.freqOI = lims(1):lims(2);
case 'ChangeTimeOI'
lims = str2double(strsplit(get(he_timeOI, 'String'),':'));
g_params.timeOI = [lims(1),lims(2)];
case 'ChangeMethod' % STFT = 1, wavelets = 2
g_params.method = c_Method_strV{get(hpm_method, 'Value')};
case 'ChangedB' % dB = 1, raw = 2
g_params.dB = c_binary_strV{get(hcm_dB, 'Value')+1};
if get(hcm_dB, 'Value')
set(he_baseline,'Enable','on');
g_params.baseline = str2double(strsplit(get(he_baseline, 'String'),':'));
else % otherwise put to zero
set(he_baseline,'Enable','off');
g_params.baseline = 0;
end
case 'ChangeBaseline'
% if dB transformation is checked use the baseline
g_params.baseline = str2double(strsplit(get(he_baseline, 'String'),':'));
case 'AdvancedSettings'
if isempty(g_EEG_dataset) % if there is no EEG dataset it won't work
set(ht_errorStatus, 'String', 'Please, load the data first!', 'ForegroundColor', [1 0 0]);
else
advanced_tf()
set(ht_errorStatus, 'String', 'Ready', 'ForegroundColor', [1 1 1]);
end
% case 'ChangeAccelerate'
% g_params.accelerate = c_binary_strV{get(hcm_accelerate, 'Value')+1};
case 'ChooseTimeLM'
g_params.time_landmarks = str2double(strsplit(get(he_timeLM, 'String'), ','));
case 'ChooseFreqLM'
g_params.freq_landmarks = str2double(strsplit(get(he_freqLM, 'String'), ','));
case 'PlotSpectra'
% check if the data is loaded
try
% isempty(g_EEG_dataset.EEG);
isempty(g_EEG_dataset.data);
catch ME
if (strcmp(ME.identifier,'MATLAB:nonExistentField')) || (strcmp(ME.identifier,'MATLAB:structRefFromNonStruct'))
set(ht_errorStatus, 'String', 'Please, load the data first!', 'ForegroundColor', [1 0 0]);
return
end
end
% go on to the analysis if the data is ready
[g_params, g_EEG_dataset] = perform_analysis(g_params, g_EEG_dataset);
% save the settings for the case of repeated analysis
g_params.previous_settings = g_params;
case 'SaveImage'
save_to = uigetdir(cd,'Navigate to the folder where the pictures will be saved');
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
image_count = 0;
for iFig = length(FigList):-1:1
FigHandle = FigList(iFig);
% image_count = image_count + 1;
if ~strcmp(FigHandle.Name,'EEG_TF') && ~strcmp(FigHandle.Name,'f_FT_SETTINGS') % save and close all the figures except the main GUI and possibly advanced settings
image_count = image_count + 1;
savefig(FigHandle, strcat(save_to, '\EEG_TF_Image', num2str(image_count), '.fig'));
saveas(FigHandle, strcat(save_to, '\EEG_TF_Image', num2str(image_count), '.jpg'));
% savefig(FigHandle, strcat(save_to, '\',g_EEG_dataset.filenames{1,FigList(iFig).Number - image_count}(1:end-4),'_EEGTF_Image', '.fig'));
% saveas(FigHandle, strcat(save_to, '\',g_EEG_dataset.filenames{1,FigList(iFig).Number - image_count}(1:end-4),'_EEGTF_Image', '.jpg'));
close(FigHandle)
end
end
case 'Quit'
% % close all the figures open, incliding the gui
% FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
% for iFig = 1:length(FigList)
% close(FigList(iFig))
% end
close all
% clear the used global variables.
gui_clearvars;
clearvars
% Use return to avoid reaching the watchoff statement at the end
return;
case 'About'
gui_help('gui_cb_about');
case 'Help'
gui_help('gui_cb_help');
end % switch
watchoff (watchonInFigure);