0001 function [eeg_data] = vb_combine_eeg_files(in_files, out_file, ext_spec)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 if ~exist('in_files', 'var'), in_files = []; end
0029 if ~exist('out_file', 'var'), out_file = ''; end
0030 if ~exist('ext_spec', 'var'), ext_spec = []; end
0031 [in_files, out_file, ext_spec] = ...
0032 inner_check_arguments(in_files, out_file, ext_spec);
0033
0034
0035
0036 n_eeg = length(in_files);
0037 if n_eeg == 0
0038 eeg_data = [];
0039 fprintf('(%s) in_files is empty\n', mfilename);
0040 return;
0041 end
0042
0043 if n_eeg == 1
0044 eeg_data = vb_load_meg_data(in_files{1});
0045 fprintf('(%s) there is nothing to do\n', mfilename);
0046 return;
0047 end
0048
0049
0050 chk_result = inner_is_combinable(in_files);
0051
0052 if ~chk_result
0053 eeg_data = [];
0054 return;
0055 end
0056
0057 last_trial = 0;
0058 n_repeat = 0;
0059
0060
0061 EEGinfo = vb_load_measurement_info(in_files{1});
0062
0063 active_trial = [];
0064 trial = [];
0065 base_file = [];
0066
0067
0068
0069 load_spec.ChannelType = 'ALL';
0070
0071 for i_eeg = 1:n_eeg
0072
0073 cur_eeg = vb_load_meg_data(in_files{i_eeg}, load_spec);
0074 cur_channel = size(cur_eeg, 1);
0075 cur_sample = size(cur_eeg, 2);
0076 cur_trial = size(cur_eeg, 3);
0077
0078 start_tr = last_trial + 1;
0079 end_tr = last_trial + cur_trial;
0080 eeg_data(:,:,start_tr:end_tr) = cur_eeg;
0081 last_trial = end_tr;
0082
0083 cur_info = vb_load_measurement_info(in_files{i_eeg});
0084 n_repeat = n_repeat + cur_info.Nrepeat;
0085
0086 if isfield(cur_info, 'Trial')
0087 trial_idx_new = start_tr : end_tr;
0088 for i = 1:cur_trial
0089 cur_info.Trial(i).number = trial_idx_new(i);
0090 end
0091 trial = [trial; cur_info.Trial];
0092 end
0093
0094 if isfield(cur_info, 'ActiveTrial')
0095 active_trial = [active_trial; cur_info.ActiveTrial];
0096 end
0097
0098 if isfield(cur_info, 'File')
0099 base_file = [base_file; cur_info.File.BaseFile];
0100 end
0101
0102 end
0103
0104
0105 EEGinfo.Nrepeat = n_repeat;
0106 EEGinfo.Trial = trial;
0107 EEGinfo.ActiveTrial = active_trial;
0108
0109 Measurement = EEGinfo.Measurement;
0110
0111 [ch_name, data_type, bin_data_dir, bin_data_dir_rel] = ...
0112 inner_solve_external_data(out_file, ext_spec, EEGinfo);
0113
0114
0115 EEGinfo.File.BaseFile = base_file;
0116
0117 [out_path, out_name] = vb_get_file_parts(out_file);
0118 EEGinfo.File.OutputDir = out_path;
0119 EEGinfo.File.EEGFile = out_name;
0120 EEGinfo.File.DataDir = bin_data_dir_rel;
0121
0122 if isempty(bin_data_dir)
0123
0124 save(out_file, 'eeg_data', 'Measurement', 'EEGinfo');
0125 else
0126 save(out_file, 'Measurement', 'EEGinfo');
0127
0128 vb_define_device;
0129 file_ext = FILE_EXT_BIN_CH_EEG;
0130
0131 n_channel = EEGinfo.Nchannel;
0132 ch_name = EEGinfo.ChannelName;
0133
0134 for i_ch = 1:n_channel
0135
0136 datafile = sprintf('%s/%s.%s', bin_data_dir, ch_name{i_ch}, file_ext);
0137 cur_fid = fopen(datafile, 'wb');
0138 if cur_fid == -1
0139 error('(%s)cannot open file (%s)', mfilename, datafile);
0140 end
0141
0142 fwrite(cur_fid, eeg_data(i_ch,:,:), data_type{i_ch});
0143 fclose(cur_fid);
0144 fprintf('--- %s\n', datafile);
0145 end
0146
0147
0148 n_data_type = length(data_type);
0149 if (n_data_type == size(eeg_data, 1)) && (n_data_type > n_channel)
0150 i_ch = i_ch + 1;
0151 datafile = sprintf('%s/%s.%s', bin_data_dir, STATUS_CH_LABEL, file_ext);
0152 cur_fid = fopen(datafile, 'wb');
0153 if cur_fid == -1
0154 error('(%s)cannot open file (%s)', mfilename, datafile);
0155 end
0156
0157 fwrite(cur_fid, eeg_data(i_ch,:,:), data_type{i_ch});
0158 fclose(cur_fid);
0159 fprintf('--- %s\n', datafile);
0160 end
0161 end
0162 return;
0163
0164
0165
0166
0167
0168
0169
0170
0171 function [in_files, out_file, ext_spec] = ...
0172 inner_check_arguments(in_files, out_file, ext_spec)
0173 func_ = mfilename;
0174
0175 if isempty(in_files)
0176 error('(%s) in_fiels is a required parameter', func_);
0177 end
0178
0179 if isempty(out_file)
0180 error('(%s) out_file is a required parameter', func_);
0181 end
0182
0183 return;
0184
0185
0186
0187
0188
0189 function [chk_result] = inner_is_combinable(in_files)
0190 func_ = mfilename;
0191 chk_result = true;
0192 n_file = length(in_files);
0193
0194
0195 for i_file = 1:n_file
0196 cur_eeg = load(in_files{i_file}, 'Measurement', 'EEGinfo');
0197
0198
0199 if ~isfield(cur_eeg, 'Measurement')
0200 fprintf('(%s) =FALSE= cannot find ''Measurement'' parameter\n', func_);
0201 chk_result = false;
0202 return;
0203 end
0204 measure{i_file} = cur_eeg.Measurement;
0205
0206 if ~isfield(cur_eeg, 'EEGinfo')
0207 fprintf('(%s) =FALSE= cannot find ''EEGinfo'' parameter\n', func_);
0208 chk_result = false;
0209 return;
0210 end
0211 eeg_info(i_file) = cur_eeg.EEGinfo;
0212
0213 end
0214
0215
0216 for i_eeg = 1:n_file
0217 if ~isfield(eeg_info(i_eeg), 'Measurement')
0218 fprintf('(%s) EEGinfo(%d) does not have ''Measurement'' field\n', ...
0219 func_, i_eeg);
0220 chk_result = false;
0221 return;
0222 end
0223 end
0224 for i_eeg = 1:n_file
0225 if ~strcmpi(measure{i_eeg}, eeg_info(i_eeg).Measurement)
0226 fprintf('(%s) Measurement is different\n', func_);
0227 chk_result = false;
0228 return;
0229 end
0230 end
0231
0232 for i_eeg = 2:n_file
0233 if ~strcmpi(measure{i_eeg-1}, measure{i_eeg})
0234 fprintf('(%s) Measurement is different\n', func_);
0235 chk_result = false;
0236 return;
0237 end
0238 end
0239
0240
0241 for i_eeg = 1:n_file
0242 if ~isfield(eeg_info(i_eeg), 'Measurement')
0243 fprintf('(%s) EEGinfo(%d) does not have ''Measurement'' field\n', ...
0244 func_, i_eeg);
0245 chk_result = false;
0246 return;
0247 end
0248 if ~isfield(eeg_info(i_eeg), 'Device')
0249 fprintf('(%s) EEGinfo(%d) does not have ''Device'' field\n', ...
0250 func_, i_eeg);
0251 chk_result = false;
0252 return;
0253 end
0254 if ~isfield(eeg_info(i_eeg), 'Nchannel')
0255 fprintf('(%s) EEGinfo(%d) does not have ''Nchannel'' field\n', ...
0256 func_, i_eeg);
0257 chk_result = false;
0258 return;
0259 end
0260 if ~isfield(eeg_info(i_eeg), 'Nsample')
0261 fprintf('(%s) EEGinfo(%d) does not have ''Nsample'' field\n', ...
0262 func_, i_eeg);
0263 chk_result = false;
0264 return;
0265 end
0266 if ~isfield(eeg_info(i_eeg), 'Nrepeat')
0267 fprintf('(%s) EEGinfo(%d) does not have ''Nrepeat'' field\n', ...
0268 func_, i_eeg);
0269 chk_result = false;
0270 return;
0271 end
0272 if ~isfield(eeg_info(i_eeg), 'Pretrigger')
0273 fprintf('(%s) EEGinfo(%d) does not have ''Pretrigger'' field\n', ...
0274 func_, i_eeg);
0275 chk_result = false;
0276 return;
0277 end
0278 if ~isfield(eeg_info(i_eeg), 'SampleFrequency')
0279 fprintf('(%s) EEGinfo(%d) does not have ''SampleFrequency'' field\n', ...
0280 func_, i_eeg);
0281 chk_result = false;
0282 return;
0283 end
0284 if ~isfield(eeg_info(i_eeg), 'Coord')
0285 fprintf('(%s) EEGinfo(%d) does not have ''Coord'' field\n', ...
0286 func_, i_eeg);
0287 chk_result = false;
0288 return;
0289 end
0290 end
0291
0292
0293
0294 for i_eeg = 2:n_file
0295 if ~strcmpi(eeg_info(i_eeg-1).Measurement, eeg_info(i_eeg).Measurement)
0296 fprintf('(%s) EEGinfo(%d).Measurement is different from EEGinfo(%d)\n', ...
0297 func_, i_eeg-1, i_eeg);
0298 chk_result = false;
0299 return;
0300 end
0301 end
0302
0303
0304 for i_eeg = 2:n_file
0305 if ~strcmpi(eeg_info(i_eeg-1).Device, eeg_info(i_eeg).Device)
0306 fprintf('(%s) EEGinfo(%d).Device is different from EEGinfo(%d)\n', ...
0307 func_, i_eeg-1, i_eeg);
0308 chk_result = false;
0309 return;
0310 end
0311 end
0312
0313
0314 for i_eeg = 2:n_file
0315 if eeg_info(i_eeg-1).Nchannel ~= eeg_info(i_eeg).Nchannel
0316 fprintf('(%s) EEGinfo(%d).Nchannel is different from EEGinfo(%d)\n', ...
0317 func_, i_eeg-1, i_eeg);
0318 chk_result = false;
0319 return;
0320 end
0321 end
0322
0323
0324 for i_eeg = 2:n_file
0325 if eeg_info(i_eeg-1).Nsample ~= eeg_info(i_eeg).Nsample
0326 fprintf('(%s) EEGinfo(%d).Nsample is different from EEGinfo(%d)\n', ...
0327 func_, i_eeg-1, i_eeg);
0328 chk_result = false;
0329 return;
0330 end
0331 end
0332
0333
0334 for i_eeg = 2:n_file
0335 if eeg_info(i_eeg-1).Pretrigger ~= eeg_info(i_eeg).Pretrigger
0336 fprintf('(%s) EEGinfo(%d).Pretrigger is different from EEGinfo(%d)\n', ...
0337 func_, i_eeg-1, i_eeg);
0338 chk_result = false;
0339 return;
0340 end
0341 end
0342
0343
0344 for i_eeg = 2:n_file
0345 if eeg_info(i_eeg-1).SampleFrequency ~= eeg_info(i_eeg).SampleFrequency
0346 fprintf( ...
0347 '(%s) EEGinfo(%d).SampleFrequency is different from EEGinfo(%d)\n', ...
0348 func_, i_eeg-1, i_eeg);
0349 chk_result = false;
0350 return;
0351 end
0352 end
0353
0354
0355 for i_eeg = 2:n_file
0356 if ~isequal(eeg_info(i_eeg-1).Coord, eeg_info(i_eeg).Coord)
0357 fprintf('(%s) EEGinfo(%d).Coord is different from EEGinfo(%d)\n', ...
0358 func_, i_eeg-1, i_eeg);
0359 chk_result = false;
0360 return;
0361 end
0362 end
0363
0364 return;
0365
0366
0367
0368
0369
0370
0371 function [ch_name, data_type, bin_data_dir, bin_data_dir_rel] = ...
0372 inner_solve_external_data(out_file, ext_spec, EEGinfo)
0373
0374 bin_data_dir = '';
0375 bin_data_dir_rel = '';
0376
0377 ch_name = '';
0378 data_type = '';
0379
0380 if isempty(out_file) || isempty(ext_spec)
0381 return;
0382 end
0383
0384 if ~isfield(ext_spec, 'bin_data_dir') || isempty(ext_spec.bin_data_dir)
0385 return;
0386 end
0387
0388 [out_path_base] = vb_get_file_parts(out_file);
0389 if isempty(out_path_base)
0390 out_path_base = '.';
0391 end
0392
0393 bin_data_dir = [out_path_base '/' ext_spec.bin_data_dir];
0394
0395 if ~isfield(EEGinfo, 'ChannelName') || isempty(EEGinfo.ChannelName)
0396
0397 bin_data_dir = [];
0398 return;
0399 end
0400 ch_name = EEGinfo.ChannelName;
0401
0402 if ~isfield(EEGinfo, 'DataType') || isempty(EEGinfo.DataType)
0403 bin_data_dir = [];
0404 return;
0405 end
0406 data_type = EEGinfo.DataType;
0407
0408 if exist(bin_data_dir, 'dir') ~= 7
0409 vb_mkdir(bin_data_dir);
0410 end
0411
0412 bin_data_dir_rel = ext_spec.bin_data_dir;
0413 return;
0414
0415
0416
0417
0418
0419
0420