forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_appenddata.m
218 lines (200 loc) · 8.32 KB
/
ft_appenddata.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
function [data] = ft_appenddata(cfg, varargin)
% FT_APPENDDATA concatenates multiple raw data structures that have been preprocessed
% separately into a single raw data structure.
%
% Use as
% data = ft_appenddata(cfg, data1, data2, data3, ...)
%
% The following configuration options are supported:
% cfg.keepsampleinfo = 'yes', 'no', 'ifmakessense' (default = 'ifmakessense')
%
% If the input datasets all have the same channels, the trials will be concatenated.
% This is useful for example if you have different experimental conditions, which,
% besides analyzing them separately, for some reason you also want to analyze
% together. The function will check for consistency in the order of the channels. If
% the order is inconsistent the channel order of the output will be according to the
% channel order of the first data structure in the input.
%
% If the input datasets have different channels, but the same number of trials, the
% channels will be concatenated within each trial. This is useful for example if the
% data that you want to analyze contains both MEG and EMG channels which require
% different preprocessing options.
%
% If you concatenate trials and the data originates from the same original datafile,
% the sampleinfo is consistent and you can specify cfg.keepsampleinfo='yes'. If the
% data originates from different datafiles, the sampleinfo is inconsistent and does
% not point to the same recording, hence you should specify cfg.keepsampleinfo='no'.
%
% Occasionally, the data needs to be concatenated in the trial dimension while
% there's a slight discrepancy in the channels in the input data (e.g. missing
% channels in one of the data structures). The function will then return a data
% structure containing only the channels which are present in all inputs.
%
% To facilitate data-handling and distributed computing you can use
% cfg.inputfile = ...
% cfg.outputfile = ...
% If you specify one of these (or both) the input data will be read from a *.mat
% file on disk and/or the output data will be written to a *.mat file. These mat
% files should contain only a single variable, corresponding with the
% input/output structure. The data structure in the input file should be a
% cell-array for this particular function.
%
% See also FT_PREPROCESSING, FT_DATAYPE_RAW, FT_APPENDTIMELOCK, FT_APPENDFREQ,
% FT_APPENDSOURCE, FT_APPENDSENS
% Copyright (C) 2005-2008, Robert Oostenveld
% Copyright (C) 2009-2011, Jan-Mathijs Schoffelen
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% these are used by the ft_preamble/ft_postamble function and scripts
ft_revision = '$Id$';
ft_nargin = nargin;
ft_nargout = nargout;
% do the general setup of the function
ft_defaults
ft_preamble init
ft_preamble debug
ft_preamble loadvar varargin
ft_preamble provenance varargin
ft_preamble trackconfig
% the ft_abort variable is set to true or false in ft_preamble_init
if ft_abort
return
end
% set the defaults
cfg.keepsampleinfo = ft_getopt(cfg, 'keepsampleinfo', 'ifmakessense');
try
% although not 100% robust, this could make some users becoming aware of the issue of overlapping trials
for i=1:numel(varargin)
dataset{i} = ft_findcfg(varargin{i}.cfg, 'dataset');
hassampleinfo(i) = isfield(varargin{i}, 'sampleinfo');
end
if ~all(strcmp(dataset, dataset{1})) && ~strcmp(cfg.keepsampleinfo, 'no')
ft_warning('the data originates from different recordings on disk');
ft_warning('please consider specifying cfg.keepsampleinfo=''no''')
end
end % try
% ensure that the input data is valid for this function
for i=1:length(varargin)
varargin{i} = ft_checkdata(varargin{i}, 'datatype', {'raw', 'raw+comp'}, 'feedback', 'no', 'hassampleinfo', cfg.keepsampleinfo);
end
% set the defaults
cfg.appendsens = ft_getopt(cfg, 'appendsens', 'no');
cfg.appenddim = ft_getopt(cfg, 'appenddim', []);
cfg.tolerance = ft_getopt(cfg, 'tolerance', 1e-5);
isequaltime = true;
isequallabel = true;
issamelabel = true;
isequalfsample = true;
for i=2:numel(varargin)
isequaltime = isequaltime && isequal(varargin{i}.time , varargin{1}.time );
isequallabel = isequallabel && isequal(varargin{i}.label, varargin{1}.label);
issamelabel = issamelabel && isempty(setxor(varargin{i}.label, varargin{1}.label));
isequalfsample = isequalfsample && isfield(varargin{i},'fsample') && isfield(varargin{1},'fsample') && isequal(varargin{i}.fsample, varargin{1}.fsample);
end
if isempty(cfg.appenddim) || strcmp(cfg.appenddim, 'auto')
if isequallabel || issamelabel
cfg.appenddim = 'rpt';
elseif isequaltime && ~isequallabel && ~issamelabel
cfg.appenddim = 'chan';
else
ft_error('cannot append this data');
end
end
ft_info('concatenating over the "%s" dimension\n', cfg.appenddim);
% ft_selectdata cannot create the union of the data contained in cell-arrays
% make a dummy without the actual data, but keep trialinfo/sampleinfo/grad/elec/opto
% also remove the topo/unmixing/topolabel, if present, otherwise it is not
% possible to concatenate raw and comp data. Note that in append_common the
% topo etc. is removed anyhow, when appenddim = 'chan'
dummy = cell(size(varargin));
for i=1:numel(varargin)
dummy{i} = removefields(varargin{i}, {'trial', 'time'});
if strcmp(cfg.appenddim, 'chan')
dummy{i} = removefields(dummy{i}, {'topo', 'unmixing', 'topolabel'});
end
% add a dummy data field, this causes the datatype to become 'chan'
dummy{i}.dummy = ones(numel(dummy{i}.label),1);
dummy{i}.dummydimord = 'chan';
end
% don't do any data appending inside the common function
cfg.parameter = {};
% use a low-level function that is shared with the other ft_appendxxx functions
% this will handle the trialinfo/sampleinfo/grad/elec/opto
data = append_common(cfg, dummy{:});
% this is the actual data field that will be appended further down
cfg.parameter = {'trial'};
switch cfg.appenddim
case 'chan'
if isequaltime
ntrl = length(varargin{1}.trial);
dat = varargin{1}.trial;
lab = varargin{1}.label(:);
for i=2:numel(varargin)
for j=1:ntrl
dat{j} = cat(1, dat{j}, varargin{i}.trial{j});
end
lab = cat(1, lab, varargin{i}.label(:));
end
data.trial = dat;
data.time = varargin{1}.time;
data.label = lab; % replace the one from append_common
else
ft_error('data has different time, cannot append over channels');
end
case 'rpt'
if isequallabel
% the channels are the same and sorted in the same order
dat = cell(1,0);
tim = cell(1,0);
for i=1:numel(varargin)
dat = cat(2, dat, varargin{i}.trial);
tim = cat(2, tim, varargin{i}.time);
end
data.trial = dat;
data.time = tim;
else
% the channels are not the same, or do not appear in the same order
% only return the intersection of the channels
dat = cell(1,0);
tim = cell(1,0);
for i=1:numel(varargin)
% find the indices of the channels in each dataset, sorted according to the first input
[dum, indx] = match_str(data.label, varargin{i}.label);
for j=1:numel(varargin{i}.trial)
dat = cat(2, dat, {varargin{i}.trial{j}(indx,:)});
tim = cat(2, tim, {varargin{i}.time{j}});
end
end
data.trial = dat;
data.time = tim;
data.label; % keep it as determined by append_common
end
otherwise
ft_error('unsupported cfg.appenddim');
end % switch
if isequalfsample
data.fsample = varargin{1}.fsample;
end
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble trackconfig
ft_postamble previous varargin
ft_postamble provenance data
ft_postamble history data
ft_postamble savevar data