0001 function vb_megfile_make_new_megfile(orgfile, newfile, loadspec, ...
0002 meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
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 if ~exist('orgfile', 'var'), orgfile = []; end
0046 if ~exist('loadspec', 'var'), loadspec = []; end
0047 if ~exist('newfile', 'var'), newfile = []; end
0048 if ~exist('meg_labels', 'var'), meg_labels = []; end
0049 if ~exist('ext_labels', 'var'), ext_labels = []; end
0050 if ~exist('ref_labels', 'var'), ref_labels = []; end
0051 if ~exist('meg', 'var'), meg = []; end
0052 if ~exist('ext', 'var'), ext = []; end
0053 if ~exist('ref', 'var'), ref = []; end
0054 if ~exist('acqtype', 'var'), acqtype = []; end
0055 if ~exist('new_samples', 'var'), new_samples = []; end
0056 [orgfile, newfile, LoadSpec, meg_labels, ext_labels, ref_labels, ...
0057 meg, ext, ref, acqtype, new_samples, data_save_ex, saveman] = ...
0058 inner_check_arguments(orgfile, newfile, loadspec, ...
0059 meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples);
0060
0061
0062
0063
0064
0065 tmp_org = load(orgfile);
0066 if isfield(tmp_org, 'ParentInfo')
0067 ParentInfo.ParentInfo = tmp_org.ParentInfo;
0068 end
0069
0070 if isfield(tmp_org, 'LoadSpec')
0071 ParentInfo.LoadSpec = tmp_org.LoadSpec;
0072 end
0073
0074
0075 ParentInfo.meg_file = orgfile;
0076
0077 clear tmp_org;
0078
0079 pick = [];
0080 Qpick = [];
0081 ref_pick = [];
0082 ref_Qpick = [];
0083 MEGinfo = [];
0084 CoordType = '';
0085 Measurement = '';
0086 PositionFile = '';
0087
0088
0089
0090 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'pick');
0091 if state == const.VALID
0092 load(orgfile, 'pick');
0093 end
0094
0095
0096 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'Qpick');
0097 if state == const.VALID
0098 load(orgfile, 'Qpick');
0099 end
0100
0101
0102 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'ref_pick');
0103 if state == const.VALID
0104 load(orgfile, 'ref_pick');
0105 end
0106
0107
0108 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'ref_Qpick');
0109 if state == const.VALID
0110 load(orgfile, 'ref_Qpick');
0111 end
0112
0113
0114 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'MEGinfo');
0115 if state == const.VALID
0116 load(orgfile, 'MEGinfo');
0117 end
0118 ParentInfo.MEGinfo = MEGinfo;
0119
0120
0121 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'CoordType');
0122 if state == const.VALID
0123 load(orgfile, 'CoordType');
0124 end
0125
0126
0127 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'Measurement');
0128 if state == const.VALID
0129 load(orgfile, 'Measurement');
0130 end
0131
0132
0133 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'PositionFile');
0134 if state == const.VALID
0135 load(orgfile, 'PositionFile');
0136 end
0137
0138
0139
0140
0141 if ~isempty(acqtype)
0142 MEGinfo = vb_meginfo_set_acqtype(MEGinfo, acqtype);
0143 end
0144
0145 Nchannel = 0;
0146 Nsample = 0;
0147 Ntrial = 0;
0148
0149 if ~isempty(meg)
0150 Nchannel = size(meg,1);
0151 Nsample = size(meg,2);
0152 Ntrial = size(meg,3);
0153 elseif ~isempty(ext)
0154 Nsample = size(ext,2);
0155 Ntrial = size(ext,3);
0156 elseif ~isempty(ref)
0157 Nsample = size(ref,2);
0158 Ntrial = size(ref,3);
0159 end
0160
0161
0162 MEGinfo = vb_meginfo_set_channel_number(MEGinfo, Nchannel);
0163
0164
0165 MEGinfo = vb_meginfo_set_sample_number(MEGinfo, Nsample);
0166
0167
0168 if ~isempty(loadspec.Pretrigger)
0169 MEGinfo = vb_meginfo_set_pre_trigger(MEGinfo, loadspec.Pretrigger);
0170 else
0171 if ~isfield(MEGinfo, 'Pretrigger') || isempty(MEGinfo.Pretrigger)
0172 MEGinfo = vb_meginfo_set_pre_trigger(MEGinfo, 0);
0173 end
0174 end
0175
0176
0177 MEGinfo = vb_info_set_Nrepeat(MEGinfo, Ntrial);
0178
0179
0180 if ~isempty(loadspec.TrialNumber)
0181
0182 old_trial = vb_info_get_trial_data(MEGinfo, loadspec.TrialNumber);
0183
0184 sample_idx = new_samples(1,1):new_samples(1,2);
0185
0186 for i_trial = 1:length(old_trial)
0187 new_trial(i_trial) = old_trial(i_trial);
0188
0189
0190 sample_old = old_trial(i_trial).sample;
0191 new_trial(i_trial).sample = sample_old(sample_idx);
0192 end
0193
0194 else
0195 old_trial = vb_info_get_trial_data(MEGinfo);
0196 n_trial_old = length(old_trial);
0197
0198 if n_trial_old == 1
0199
0200 n_trial_new = size(new_samples, 1);
0201
0202 for i_trial = 1:n_trial_new
0203 sample_idx = new_samples(i_trial,1):new_samples(i_trial,2);
0204 new_trial(i_trial).sample = sample_idx;
0205 new_trial(i_trial).number = i_trial;
0206 new_trial(i_trial).Active = true;
0207 end
0208 else
0209
0210 sample_idx = new_samples(1,1):new_samples(1,2);
0211
0212 for i_trial = length(old_trial)
0213 new_trial(i_trial) = old_trial(i_trial);
0214
0215
0216 sample_old = old_trial(i_trial).sample;
0217 new_trial(i_trial).sample = sample_old(sample_idx);
0218 end
0219 end
0220 end
0221
0222 MEGinfo.Trial = vb_util_arrange_list(new_trial, 0);
0223
0224 MEGinfo = vb_info_adjust_trial(MEGinfo);
0225
0226
0227 load_ch_name = vb_loadspec_get_channel_name(loadspec);
0228
0229 if ~isempty(load_ch_name)
0230
0231 [ch_idx] = ...
0232 vb_meginfo_get_channel_index_meg(MEGinfo, load_ch_name);
0233 MEGinfo.MEGch_id = MEGinfo.MEGch_id(ch_idx);
0234
0235
0236 MEGinfo.MEGch_name = MEGinfo.MEGch_name(ch_idx);
0237
0238
0239 active_ch = vb_info_get_active_channel(MEGinfo);
0240 if isempty(active_ch)
0241 Nch = length(MEGinfo.MEGch_id);
0242 MEGinfo = vb_info_set_active_channel(MEGinfo, ones(Nch,1));
0243 elseif length(active_ch) ~= length(MEGinfo.MEGch_id)
0244 MEGinfo = vb_info_set_active_channel(MEGinfo, active_ch(ch_idx));
0245 end
0246
0247
0248
0249
0250
0251 new_channel_info = vb_megfile_make_channel_info(orgfile, ...
0252 meg_labels, [], []);
0253
0254 else
0255 MEGinfo.MEGch_id = [];
0256 MEGinfo.MEGch_name = '';
0257 MEGinfo = vb_info_set_active_channel(MEGinfo, []);
0258 new_channel_info = [];
0259 ch_idx = [];
0260 end
0261
0262 MEGinfo = vb_info_set_channel_info(MEGinfo, new_channel_info);
0263
0264
0265 if ~isempty(pick) && ~isempty(Qpick)
0266 [pick, Qpick, MEGinfo] = vb_meg_adjust_sensor(pick, Qpick, ch_idx, MEGinfo);
0267 end
0268
0269
0270 if ~isempty(ref_pick) && ~isempty(ref_Qpick)
0271 [MEGinfo, ref_pick, ref_Qpick] = ...
0272 vb_meginfo_solve_extra_channel(MEGinfo, ref_pick, ref_Qpick, load_ch_name);
0273 end
0274
0275
0276
0277 if ~data_save_ex
0278 bexp = meg;
0279 bexp_ext = ext;
0280 refmg = ref;
0281 MEGinfo.saveman = [];
0282
0283 else
0284 const = vb_define_yokogawa;
0285 data_ext = const.EXT_CHANNEL_BIN;
0286
0287 bexp = [];
0288 bexp_ext = [];
0289 refmg = [];
0290
0291 f_path = vb_get_file_parts(newfile);
0292 d_path = fullfile(f_path, vb_saveman_get_dir(saveman));
0293
0294 prec = vb_saveman_get_precision(saveman);
0295
0296
0297 n_ch = length(meg_labels);
0298 for i_ch = 1:n_ch
0299 vb_util_make_external_data_file(d_path, meg_labels{i_ch}, ...
0300 data_ext, prec, meg(i_ch,:,:) );
0301 end
0302
0303
0304 n_ch = length(ext_labels);
0305 for i_ch = 1:n_ch
0306 vb_util_make_external_data_file(d_path, ext_labels{i_ch}, ...
0307 data_ext, prec, ext(i_ch,:,:) );
0308 end
0309
0310
0311 n_ch = length(ref_labels);
0312 for i_ch = 1:n_ch
0313 vb_util_make_external_data_file(d_path, ref_labels{i_ch}, ...
0314 data_ext, prec, ref(i_ch,:,:) );
0315 end
0316
0317 MEGinfo.saveman = saveman;
0318 end
0319
0320 vb_fsave(newfile, 'pick', 'Qpick', 'ref_pick', 'ref_Qpick', 'CoordType', ...
0321 'bexp', 'bexp_ext', 'refmg', 'LoadSpec', 'ParentInfo', ...
0322 'MEGinfo', 'Measurement', 'PositionFile');
0323
0324 return;
0325
0326
0327
0328
0329
0330
0331
0332 function [orgfile, newfile, loadspec, meg_labels, ext_labels, ref_labels, ...
0333 meg, ext, ref, acqtype, new_samples, data_save_ex, saveman] = ...
0334 inner_check_arguments(orgfile, newfile, loadspec, ...
0335 meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
0336 func_ = mfilename;
0337 if isempty(orgfile)
0338 error('(%s)orgfile is a required parameter', func_);
0339 end
0340
0341 if exist(orgfile, 'file') ~= 2
0342 error('(%s)cannot find orgfile : %s', func_, orgfile);
0343 end
0344
0345 if isempty(newfile)
0346 error('(%s)newfile is a required parameter', func_);
0347 end
0348 if isempty(loadspec)
0349 error('(%s)loadspec is a required parameter', func_);
0350 end
0351
0352
0353 data_save_ex = 0;
0354 saveman = [];
0355 if isfield(loadspec, 'saveman')
0356 saveman = loadspec.saveman;
0357 data_save_ex = vb_saveman_get_switch(loadspec.saveman);
0358 end
0359
0360 if isempty(meg_labels)
0361
0362 end
0363 if isempty(ext_labels)
0364
0365 end
0366 if isempty(ref_labels)
0367
0368 end
0369
0370 if isempty(acqtype)
0371
0372 end
0373 if isempty(new_samples)
0374 warning('(%s)new_samples is not specified\n', func_);
0375 end
0376 return;
0377
0378
0379
0380
0381
0382