0001 function vb_megfile_inherit_gene(orgfile, newfile, ...
0002 meg, ext, ref, saveman, meg_labels, ext_labels, ref_labels)
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 if ~exist('orgfile', 'var'), orgfile = []; end
0039 if ~exist('newfile', 'var'), newfile = []; end
0040 if ~exist('meg', 'var'), meg = []; end
0041 if ~exist('ext', 'var'), ext = []; end
0042 if ~exist('ref', 'var'), ref = []; end
0043
0044 if ~exist('saveman', 'var'), saveman = []; end
0045
0046 if ~exist('meg_labels', 'var'), meg_labels = []; end
0047 if ~exist('ext_labels', 'var'), ext_labels = []; end
0048 if ~exist('ref_labels', 'var'), ref_labels = []; end
0049 [orgfile, newfile, meg, ext, ref, ...
0050 saveman, meg_labels, ext_labels, ref_labels] = ...
0051 inner_check_arguments(orgfile, newfile, meg, ext, ref, ...
0052 saveman, meg_labels, ext_labels, ref_labels);
0053
0054
0055
0056 func_ = mfilename;
0057
0058 org = load(orgfile);
0059
0060
0061 MEGinfo = org.MEGinfo;
0062
0063 Nchannel_meg = 0;
0064 Nchannel_ext = 0;
0065 Nchannel_ref = 0;
0066
0067 Nsample_meg = 0;
0068 Nsample_ext = 0;
0069 Nsample_ref = 0;
0070
0071 Ntrial_meg = 0;
0072 Ntrial_ext = 0;
0073 Ntrial_ref = 0;
0074
0075 if ~isempty(meg)
0076 Nchannel_meg = size(meg, 1);
0077 Nsample_meg = size(meg, 2);
0078 Ntrial_meg = size(meg, 3);
0079 end
0080
0081 if ~isempty(ext)
0082 Nchannel_ext = size(ext, 1);
0083 Nsample_ext = size(ext, 2);
0084 Ntrial_ext = size(ext, 3);
0085 end
0086
0087 if ~isempty(ref)
0088 Nchannel_ref = size(ref, 1);
0089 Nsample_ref = size(ref, 2);
0090 Ntrial_ref = size(ref, 3);
0091 end
0092
0093
0094
0095
0096 if ~iscell(meg_labels), meg_labels = {meg_labels}; end
0097 if ~iscell(ext_labels), ext_labels = {ext_labels}; end
0098 if ~iscell(ref_labels), ref_labels = {ref_labels}; end
0099
0100 if Nchannel_meg ~= length(meg_labels)
0101 error('(%s) Nchannel_meg is incorrect (%d, %d)', ...
0102 func_, Nchannel_meg, length(meg_labels));
0103 end
0104
0105 if Nchannel_ext ~= length(ext_labels)
0106 error('(%s) Nchannel_ext is incorrect (%d, %d)', ...
0107 func_, Nchannel_ext, length(ext_labels));
0108 end
0109
0110 if Nchannel_ref ~= length(ref_labels)
0111 error('(%s) Nchannel_ref is incorrect (%d, %d)', ...
0112 func_, Nchannel_ref, length(ref_labels));
0113 end
0114
0115
0116 if ~(Nsample_meg == Nsample_ext && Nsample_meg == Nsample_ref)
0117 error('(%s) the number of sample must be the same (%d, %d, %d)', ...
0118 func_, Nsample_meg, Nsample_ext, Nsample_ref);
0119 end
0120
0121
0122 if ~(Ntrial_meg == Ntrial_ext && Ntrial_meg == Ntrial_ref)
0123 error('(%s) the number of trial must be the same (%d, %d, %d)', ...
0124 func_, Ntrial_meg, Ntrial_ext, Ntrial_ref);
0125 end
0126
0127
0128 Nsample_new = Nsample_meg;
0129 Ntrial_new = Ntrial_meg;
0130
0131
0132 MEGinfo = vb_meginfo_set_channel_number(MEGinfo, Nchannel_meg);
0133
0134
0135 MEGinfo = vb_meginfo_set_sample_number(MEGinfo, Nsample_new);
0136
0137
0138 MEGinfo = vb_info_set_Nrepeat(MEGinfo, Ntrial_new);
0139
0140
0141
0142
0143 [ch_idx] = ...
0144 vb_meginfo_get_channel_index_meg(MEGinfo, meg_labels);
0145 MEGinfo.MEGch_id = MEGinfo.MEGch_id(ch_idx);
0146
0147
0148 MEGinfo.MEGch_name = MEGinfo.MEGch_name(ch_idx);
0149
0150
0151 active_ch = vb_info_get_active_channel(MEGinfo);
0152 if isempty(active_ch)
0153 Nch = length(MEGinfo.MEGch_id);
0154 MEGinfo = vb_info_set_active_channel(MEGinfo, ones(Nch,1));
0155 elseif length(active_ch) ~= length(MEGinfo.MEGch_id)
0156 MEGinfo = vb_info_set_active_channel(MEGinfo, active_ch(ch_idx));
0157 end
0158
0159
0160
0161
0162
0163 new_channel_info = vb_megfile_make_channel_info(orgfile, meg_labels, [], []);
0164 MEGinfo = vb_info_set_channel_info(MEGinfo, new_channel_info);
0165
0166
0167
0168
0169 if ~isempty(org.pick) && ~isempty(org.Qpick)
0170 [org.pick, org.Qpick, MEGinfo] = ...
0171 vb_meg_adjust_sensor(org.pick, org.Qpick, ch_idx, MEGinfo);
0172 end
0173
0174
0175 all_ex_label = [ext_labels; ref_labels];
0176 [MEGinfo, org.ref_pick, org.ref_Qpick] = ...
0177 vb_meginfo_solve_extra_channel( MEGinfo, ...
0178 org.ref_pick, org.ref_Qpick, all_ex_label);
0179
0180 do_save_ext = vb_saveman_get_switch(saveman);
0181 new_meg = org;
0182 new_meg.MEGinfo = MEGinfo;
0183
0184 if ~do_save_ext
0185 new_meg.bexp = meg;
0186 new_meg.bexp_ext = ext;
0187 new_meg.refmg = ref;
0188 end
0189
0190
0191 vb_save_struct(newfile, new_meg);
0192
0193 if do_save_ext
0194 const = vb_define_yokogawa;
0195
0196 f_path = vb_get_file_parts(newfile);
0197 b_path = vb_saveman_get_dir(saveman);
0198 PRECISION = vb_saveman_get_precision(saveman);
0199
0200 if exist([f_path '/' b_path], 'dir') ~= 7
0201 mkdir([f_path '/' b_path]);
0202 end
0203
0204
0205 for i_ch = 1:Nchannel_meg
0206 inner_make_external_data_file( ...
0207 f_path, b_path, meg_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0208 PRECISION, meg(i_ch,:,:));
0209 end
0210
0211
0212 for i_ch = 1:Nchannel_ext
0213 inner_make_external_data_file( ...
0214 f_path, b_path, ext_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0215 PRECISION, ext(i_ch,:,:));
0216 end
0217
0218
0219 for i_ch = 1:Nchannel_ref
0220 inner_make_external_data_file( ...
0221 f_path, b_path, ref_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0222 PRECISION, ref(i_ch,:,:));
0223 end
0224 end
0225 return;
0226
0227
0228
0229
0230
0231
0232
0233 function [orgfile, newfile, meg, ext, ref, ...
0234 saveman, meg_labels, ext_labels, ref_labels] = ...
0235 inner_check_arguments(orgfile, newfile, meg, ext, ref, ...
0236 saveman, meg_labels, ext_labels, ref_labels)
0237 func_ = mfilename;
0238 if isempty(orgfile)
0239 error('(%s)orgfile is a required parameter', func_);
0240 end
0241
0242 if exist(orgfile, 'file') ~= 2
0243 error('(%s)cannot find orgfile : %s', func_, orgfile);
0244 end
0245
0246 if isempty(newfile)
0247 error('(%s)newfile is a required parameter', func_);
0248 end
0249
0250 if isempty(meg)
0251 error('(%s)meg is a required parameter', func_);
0252 end
0253
0254 if isempty(ext)
0255 error('(%s)ext is a required parameter', func_);
0256 end
0257
0258 if isempty(ref)
0259 error('(%s)ref is a required parameter', func_);
0260 end
0261
0262 if isempty(saveman)
0263
0264 end
0265
0266 if isempty(meg_labels)
0267 meg_labels = vb_megfile_get_channel_label_meg(orgfile);
0268 end
0269 if isempty(ext_labels)
0270 ext_labels = vb_megfile_get_channel_label_extra(orgfile);
0271 end
0272 if isempty(ref_labels)
0273 ref_labels = vb_megfile_get_channel_label_refmg(orgfile);
0274 end
0275 return;
0276
0277
0278
0279
0280
0281 function inner_make_external_data_file(fpath, d_dir, fname, f_ext, prec, data)
0282 func_ = mfilename;
0283
0284 ch_file = sprintf('%s/%s/%s.%s', fpath, d_dir, fname, f_ext);
0285 fid = fopen(ch_file, 'wb');
0286 if fid == -1
0287 error('(%s)cannot open file (%s)', func_, ch_file);
0288 end
0289
0290 fwrite(fid, data(:), prec);
0291 fclose(fid);
0292 return;
0293
0294
0295
0296
0297
0298