0001 function [data, ch_info] = vb_msrmnt_make_trial_data(meeg_file, proc_spec, new_meeg_file)
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
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 if ~exist('proc_spec', 'var'), proc_spec = []; end
0058 if ~exist('meeg_file', 'var'), meeg_file = ''; end
0059 if ~exist('new_meeg_file', 'var'), new_meeg_file = ''; end
0060
0061 [meeg_file, proc_spec, new_meeg_file] = ...
0062 inner_check_arguments(meeg_file, proc_spec, new_meeg_file);
0063
0064
0065
0066
0067
0068 [base_file_def, trigger_def, cond_id_def, pretrigger_def, posttrigger_def, ...
0069 sample_freq_def] = inner_read_trigger_file(proc_spec.trig_file);
0070
0071
0072 [base_file_sub, trigger_sub, cond_id_sub, pretrigger_sub, posttrigger_sub, ...
0073 sample_freq_sub] = inner_read_proc_parm(meeg_file, proc_spec.proc_parm);
0074
0075 if ~isempty(trigger_sub)
0076 trigger = trigger_sub;
0077 else
0078 trigger = trigger_def;
0079 end
0080
0081 if ~isempty(cond_id_sub)
0082 cond_id = cond_id_sub;
0083 else
0084 cond_id = cond_id_def;
0085 end
0086
0087 if ~isempty(pretrigger_sub)
0088 pretrigger_ms = pretrigger_sub;
0089 else
0090 pretrigger_ms = pretrigger_def;
0091 end
0092
0093 if ~isempty(posttrigger_sub)
0094 posttrigger_ms = posttrigger_sub;
0095 else
0096 posttrigger_ms = posttrigger_def;
0097 end
0098
0099
0100 if ~isempty(sample_freq_sub)
0101 sample_freq = sample_freq_sub;
0102 else
0103 sample_freq = sample_freq_def;
0104 end
0105
0106
0107 info = vb_load_measurement_info(meeg_file);
0108 Measurement = vb_info_get_measurement(info);
0109
0110
0111 if strcmp(Measurement, 'MEG')
0112 [f_, p_, e_] = vb_get_file_parts(new_meeg_file);
0113 new_spec.trigger = trigger;
0114 new_spec.cond_id = cond_id;
0115 new_spec.pretrigger_ms = pretrigger_ms;
0116 new_spec.posttrigger_ms = posttrigger_ms;
0117 new_spec.sample_freq = sample_freq;
0118 new_spec.root_dir = f_;
0119 new_spec.new_file = [p_, e_];
0120 new_spec.new_bin_dir = proc_spec.new_bin_dir;
0121 [data, ch_info] = vb_megfile_make_trial_data(meeg_file, new_spec);
0122 else
0123
0124
0125
0126
0127
0128 info = vb_load_measurement_info(meeg_file);
0129 fs = vb_info_get_sampling_frequency(info);
0130
0131
0132 load_spec.Trigger = round(trigger * (fs/sample_freq));
0133
0134
0135 load_spec.Pretrigger = ceil(pretrigger_ms *(fs/1000));
0136 load_spec.Posttrigger = ceil(posttrigger_ms *(fs/1000));
0137
0138 load_spec.ChannelType = 'ALL';
0139 load_spec.ActiveChannel = false;
0140
0141 [data, ch_info] = vb_load_meg_data(meeg_file, load_spec, new_meeg_file);
0142
0143
0144 load(new_meeg_file, 'EEGinfo');
0145 EEGinfo.cond_id = cond_id;
0146 vb_save(new_meeg_file, 'EEGinfo');
0147 end
0148
0149
0150
0151
0152
0153
0154
0155 function [meeg_file, proc_spec, new_meeg_file] = ...
0156 inner_check_arguments(meeg_file, proc_spec, new_meeg_file)
0157 func_ = mfilename;
0158
0159 if isempty(meeg_file)
0160 error('(%s) meeg_file is a required parameter', func_);
0161 end
0162 if exist(meeg_file, 'file') ~= 2
0163 error('(%s) Specified meeg_file not found.', func_);
0164 end
0165 if isempty(strfind(meeg_file, '.meg.mat')) && ...
0166 isempty(strfind(meeg_file, '.eeg.mat'))
0167 error('(%s) meeg_file is not MEG(.meg.mat) or EEG(.eeg.mat) file.', func_);
0168 end
0169
0170 if isempty(proc_spec)
0171 error('(%s) proc_spec is a required parameter', func_);
0172 end
0173
0174 if isempty(new_meeg_file)
0175 error('(%s) new_meeg_file is a required parameter', func_);
0176 end
0177
0178
0179
0180
0181
0182
0183 if ~isfield(proc_spec, 'trig_file')
0184 proc_spec.trig_file = '';
0185 end
0186
0187 if ~isfield(proc_spec, 'internal')
0188 proc_spec.internal = true;
0189 end
0190
0191 if proc_spec.internal == false
0192 [p_, f_, e_] = vb_get_file_parts(new_meeg_file);
0193 new_bin_dir = f_(1:end-4);
0194 proc_spec.new_bin_dir = ['./' new_bin_dir];
0195 else
0196 proc_spec.new_bin_dir = '';
0197 end
0198
0199 if ~isfield(proc_spec, 'proc_parm')
0200 proc_spec.proc_parm = [];
0201 end
0202
0203 if isempty(proc_spec.trig_file) && isempty(proc_spec.proc_parm)
0204 error('(%s) either trig_file or proc_parm is required', func_);
0205 end
0206 return;
0207
0208
0209
0210
0211
0212
0213 function [base_file, trigger, cond_id, pretrigger, posttrigger, sample_freq] = ...
0214 inner_read_trigger_file(trig_file)
0215
0216 func_ = mfilename;
0217
0218 base_file = '';
0219 trigger = [];
0220 cond_id = [];
0221 pretrigger = [];
0222 posttrigger = [];
0223 sample_freq = 1024;
0224
0225 if isempty(trig_file)
0226 return;
0227 end
0228
0229 if exist(trig_file, 'file') ~= 2
0230 error('(%s) cannot find trig_file : %s', func_, trig_file);
0231 end
0232
0233 trig = load(trig_file);
0234 if isfield(trig, 'trig') && ~isempty(trig.trig)
0235 trigger = trig.trig;
0236 end
0237 if isfield(trig, 'cond_id') && ~isempty(trig.cond_id)
0238 cond_id = trig.cond_id;
0239 end
0240
0241 if isfield(trig, 'parm') && ~isempty(trig.parm)
0242 if isfield(trig.parm, 'data_file') && ~isempty(trig.parm.data_file)
0243 base_file = trig.parm.data_file;
0244 end
0245
0246 if isfield(trig.parm, 'Pretrigger_ms')
0247 pretrigger = trig.parm.Pretrigger_ms;
0248 end
0249
0250 if isfield(trig.parm, 'Posttrigger_ms')
0251 posttrigger = trig.parm.Posttrigger_ms;
0252 end
0253
0254 if isfield(trig.parm, 'fsamp')
0255 sample_freq = trig.parm.fsamp;
0256 end
0257 end
0258 return;
0259
0260
0261
0262
0263
0264
0265 function [base_file, trigger, cond_id, pretrigger, posttrigger, sample_freq] = ...
0266 inner_read_proc_parm(meeg_file, parm)
0267
0268 base_file = '';
0269 trigger = [];
0270 cond_id = [];
0271 pretrigger = [];
0272 posttrigger = [];
0273 sample_freq = [];
0274
0275 if isempty(parm)
0276 return;
0277 end
0278
0279 if isfield(parm, 'trigger') && ~isempty(parm.trigger)
0280 trigger = parm.trigger;
0281 end
0282 if isfield(parm, 'cond_id') && ~isempty(parm.cond_id)
0283 cond_id = parm.cond_id;
0284 end
0285
0286 [p_, f_, e_] = vb_get_file_parts(meeg_file);
0287 base_file = [f_, e_];
0288
0289 if isfield(parm, 'Pretrigger_ms')
0290 pretrigger = parm.Pretrigger_ms;
0291 end
0292
0293 if isfield(parm, 'Posttrigger_ms')
0294 posttrigger = parm.Posttrigger_ms;
0295 end
0296
0297 if isfield(parm, 'sample_freq')
0298 sample_freq = parm.sample_freq;
0299 end
0300 return;
0301
0302
0303
0304
0305
0306