forked from cab79/Matlab_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgplotprepare_eeglabdata_from_spm.m
63 lines (58 loc) · 2.14 KB
/
gplotprepare_eeglabdata_from_spm.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
function E=gplotprepare_eeglabdata_from_spm(S,D)
%% load EEGLAB data using filenames from an SPM file and reformat for plotting topography
% requires P containing fields:
% P.spm_path
% P.eeglab_path
% P.eventtypes
% P.savename
% P.st_string
% P.en_string
% load SPM
load(fullfile(D.spm_path,'SPM.mat'));
imglist = SPM.xY.P; % Subject-condition image list
fnamesdesign={};
for i = 1:length(imglist);
st=strfind(imglist{i},S.st_string);
en=strfind(imglist{i},S.en_string);
fnamesdesign{i,1} =imglist{i}(st+length(S.st_string):en-1);
%get imglist condition order
[~,nme,~] = fileparts(imglist{i});
C=strsplit(nme,'_');
condlabelsdesign(i,1) = str2num(C{2});
end
condlabels = unique(condlabelsdesign,'stable');
condlabels = condlabels(ismember(condlabels,S.eventtypes));
[fnames,~,fni] = unique(fnamesdesign,'stable');
D.DAT=cell(length(imglist),1);
D.filenames=cell(length(imglist),2)
for f = 1:length(fnames)
EEGall=pop_loadset([fnames{f} '.set'],S.eeglab_path);
% obtain trial indices from EGI data and flip chans if needed
if any(find(strcmp('STIM',EEGall.epoch(1).eventtype)))
[conds, tnums, fnums, bnums] = get_markers(EEGall);
if S.use_flipped
EEGflip = flipchanEGI(EEGall);
for i = length(S.flipcond)
trialind = find(ismember(conds,S.flipcond(i)));
EEGall.data(:,:,trialind)= EEGflip.data(:,:,trialind);
end
clear EEGflip
end
end
filecondind = find(fni==f);
for e = 1:length(condlabels)
if iscell(condlabels) && any(strcmp({EEGall.event.type},condlabels{e}))
EEG = pop_selectevent(EEGall,'type',condlabels{e});
D.DAT{filecondind(e),1} = mean(EEG.data,3);
elseif exist('conds','var') % EGI
EEG = pop_select(EEGall,'trial',find(ismember(conds,S.mark{e})));
D.DAT{filecondind(e),1} = mean(EEG.data,3);
end
D.filenames(filecondind(e),:) = {fnames{f},condlabels(e)};
end
end
D.DAT=reshape(D.DAT,[],1);
D.EEGtimes = EEGall.times;
D.chanlocs=EEGall.chanlocs;
E=D;
save(fullfile(S.eeglab_path,S.ERPsavename),'E');