0001 function vb_megfile_import_extra_data(megfile, extdata, ch_info, ow_flag, newfile)
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 if ~exist('megfile', 'var'), megfile = ''; end
0042 if ~exist('extdata', 'var'), extdata = []; end
0043 if ~exist('ch_info', 'var'), ch_info = []; end
0044 if ~exist('ow_flag', 'var'), ow_flag = []; end
0045 if ~exist('newfile', 'var'), newfile = ''; end
0046 [megfile, extdata, ch_info, ow_flag, newfile] = ...
0047 inner_check_arguments(megfile, extdata, ch_info, ow_flag, newfile);
0048
0049
0050
0051 func_ = mfilename;
0052
0053 org_info = vb_load_measurement_info(megfile);
0054 n_sp_org = vb_info_get_sample_number(org_info);
0055 n_tr_org = vb_info_get_trial_number(org_info);
0056
0057 [n_ch_ext, n_sp_ext, n_tr_ext] = size(extdata);
0058
0059
0060 if n_sp_org ~= n_sp_ext
0061 error('(%s) number of sample must be the same (%d, %d)', ...
0062 func_, n_sp_org, n_sp_ext);
0063 end
0064
0065 if n_tr_org ~= n_tr_ext
0066 error('(%s) number of trial must be the same (%d, %d)', ...
0067 func_, n_tr_org, n_tr_ext);
0068 end
0069
0070
0071
0072 idx_name_meg = [];
0073 idx_name_ext = [];
0074
0075
0076 org_meg_name_list = org_info.MEGch_name;
0077 if ~isempty(org_meg_name_list)
0078 [result_name_meg, idx_name_meg] = vb_util_check_string_lists( ...
0079 org_meg_name_list, ch_info.Name);
0080
0081 if ~isempty(idx_name_meg) && ow_flag == 0
0082 for i_ch = 1:length(idx_name_meg)
0083 fprintf('(%s)', org_meg_name_list{idx_name_meg(i_ch)});
0084 end
0085 error('(%s) some ch_info.Name has been always used', func_);
0086 end
0087 end
0088
0089
0090 org_ext_name_list = org_info.ExtraChannelInfo.Channel_name;
0091 if ~isempty(org_ext_name_list)
0092 [result_name_ext, idx_name_ext] = vb_util_check_string_lists( ...
0093 org_ext_name_list, ch_info.Name);
0094
0095 if ~isempty(idx_name_ext) && ow_flag == 0
0096 for i_ch = 1:length(idx_name_ext)
0097 fprintf('(%s)', org_ext_name_list{idx_name_ext(i_ch)});
0098 end
0099 error('(%s) some ch_info.Name has been always used', func_);
0100 end
0101 end
0102
0103 if isempty(idx_name_meg) && isempty(idx_name_ext) ...
0104
0105 ow_flag = 0;
0106 end
0107
0108
0109 org_data = load(megfile);
0110 const = vb_define_yokogawa;
0111 data_dir = vb_saveman_get_dir(org_info.saveman);
0112
0113
0114 if ow_flag == 0
0115
0116
0117 [org_ext_info] = inner_update_extra_ch_info(org_data.MEGinfo, ch_info);
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 org_data.MEGinfo.ExtraChannelInfo = org_ext_info;
0151
0152
0153 if inner_is_stored_externally(megfile)
0154
0155
0156 f_path = vb_get_file_parts(newfile);
0157 PRECISION = vb_meginfo_get_precision(org_info);
0158
0159 for i_ch = 1:n_ch_ext
0160 inner_make_external_data_file( ...
0161 f_path, data_dir, ch_info.Name{i_ch}, const.EXT_CHANNEL_BIN, ...
0162 PRECISION, extdata(i_ch,:,:));
0163 end
0164
0165 else
0166
0167 for i_ch = 1:n_ch_ext
0168 org_data.bexp_ext = [org_data.bexp_ext; extdata(i_ch,:,:)];
0169 end
0170 end
0171
0172
0173 else
0174
0175
0176
0177 ch_info_idx_meg = [];
0178 ch_info_idx_ext = [];
0179 ch_info_idx_new = [];
0180
0181 info = [];
0182 if ~isempty(idx_name_meg)
0183
0184 ow_name = org_meg_name_list(idx_name_meg);
0185
0186
0187 [res_tmp, idx_ext] = vb_util_check_string_lists(ch_info.Name, ow_name);
0188 ch_info_idx_meg = idx_ext;
0189
0190
0191 info = org_data.MEGinfo;
0192 for i_ch = 1:length(idx_name_meg)
0193 idx_org = idx_name_meg(i_ch);
0194 idx_new = idx_ext(i_ch);
0195 info.ActiveChannel(idx_org) = ch_info.Active(idx_new);
0196 info.ChannelInfo.Active(idx_org) = ch_info.Active(idx_new);
0197 info.ChannelInfo.Type(idx_org) = ch_info.Type(idx_new);
0198 end
0199 end
0200
0201 if ~isempty(info)
0202 org_data.MEGinfo = info;
0203 info = [];
0204 end
0205
0206 if ~isempty(idx_name_ext)
0207
0208 ow_name = org_ext_name_list(idx_name_ext);
0209
0210
0211 [res_tmp, idx_ext] = vb_util_check_string_lists(ch_info.Name, ow_name);
0212 ch_info_idx_ext = idx_ext;
0213
0214
0215 info = org_data.MEGinfo.ExtraChannelInfo;
0216 for i_ch = 1:length(idx_name_ext)
0217 idx_org = idx_name_ext(i_ch);
0218 idx_new = idx_ext(i_ch);
0219 info.Channel_active(idx_org) = ch_info.Active(idx_new);
0220 info.Channel_type(idx_org) = ch_info.Type(idx_new);
0221 end
0222 end
0223
0224 if ~isempty(info)
0225 org_data.MEGinfo.ExtraChannelInfo = info;
0226 info = [];
0227 end
0228
0229
0230 whole_list = [org_meg_name_list; org_ext_name_list];
0231 [new_list, idx] = vb_util_omit_list(ch_info.Name, whole_list);
0232 ch_info_idx_new = idx;
0233
0234 cur_ch_info.Name = new_list;
0235 cur_ch_info.Active = ch_info.Active(idx);
0236 cur_ch_info.Type = ch_info.Type(idx);
0237
0238 [ext_info] = inner_update_extra_ch_info(org_data.MEGinfo, cur_ch_info);
0239 org_data.MEGinfo.ExtraChannelInfo = ext_info;
0240
0241
0242 if inner_is_stored_externally(megfile)
0243
0244
0245 f_path = vb_get_file_parts(newfile);
0246 PRECISION = vb_meginfo_get_precision(org_info);
0247
0248 for i_ch = 1:n_ch_ext
0249 inner_make_external_data_file( ...
0250 f_path, data_dir, ch_info.Name{i_ch}, const.EXT_CHANNEL_BIN, ...
0251 PRECISION, extdata(i_ch,:,:));
0252 end
0253
0254 else
0255
0256 if ~isempty(ch_info_idx_meg)
0257
0258 org_data.bexp(idx_name_meg,:,:) = extdata(ch_info_idx_meg,:,:);
0259 end
0260
0261 if ~isempty(ch_info_idx_ext)
0262
0263 org_data.bexp_ext(idx_name_ext,:,:) = extdata(ch_info_idx_ext,:,:);
0264 end
0265
0266 if ~isempty(ch_info_idx_new)
0267
0268 org_data.bexp_ext = [org_data.bexp_ext; extdata(ch_info_idx_new,:,:)];
0269 end
0270 end
0271 end
0272
0273
0274 vb_save_struct(newfile, org_data);
0275 return;
0276
0277
0278
0279
0280
0281
0282
0283 function [megfile, extdata, ch_info, ow_flag, newfile] = ...
0284 inner_check_arguments(megfile, extdata, ch_info, ow_flag, newfile)
0285
0286 func_ = mfilename;
0287
0288 if isempty(megfile)
0289 error('(%s) megfile is a required parameter', func_);
0290 end
0291
0292 if exist(megfile, 'file') ~= 2
0293 error('(%s) cannot find megfile %s', func_, megfile);
0294 end
0295
0296
0297 if isempty(extdata)
0298 error('(%s) extdata is a required parameter', func_);
0299 end
0300
0301
0302 if isempty(ch_info)
0303 error('(%s) ch_info is a required parameter', func_);
0304
0305 elseif ~isfield(ch_info, 'Active')
0306 ch_info.Active = 1;
0307
0308 elseif ~isfield(ch_info, 'Type')
0309 ch_info.Type = -9999;
0310
0311 elseif ~isfield(ch_info, 'Name') || isempty(ch_info.Name)
0312 error('(%s) Name is a required field of ch_info', func_);
0313 end
0314
0315
0316 if isempty(ow_flag)
0317 ow_flag = 0;
0318 end
0319
0320
0321 if isempty(newfile)
0322 newfile = megfile;
0323 end
0324 return;
0325
0326
0327
0328
0329
0330 function [result] = inner_is_stored_externally(megfile)
0331
0332 result = 0;
0333
0334 [state_bexp, guide_const] = vb_util_check_variable_in_matfile(megfile, 'bexp');
0335 [state_bexp_ext] = vb_util_check_variable_in_matfile(megfile, 'bexp_ext');
0336 [state_refmg] = vb_util_check_variable_in_matfile(megfile, 'refmg');
0337
0338 if state_bexp ~= guide_const.VALID ...
0339 && state_bexp_ext ~= guide_const.VALID ...
0340 && state_refmg ~= guide_const.VALID
0341
0342 result = 1;
0343 else
0344
0345 result = 0;
0346 end
0347 return;
0348
0349
0350
0351
0352
0353 function inner_make_external_data_file(fpath, d_dir, fname, f_ext, prec, data)
0354 func_ = mfilename;
0355
0356 ch_file = sprintf('%s/%s/%s.%s', fpath, d_dir, fname, f_ext);
0357 fid = fopen(ch_file, 'wb');
0358 if fid == -1
0359 error('(%s)cannot open file (%s)', func_, ch_file);
0360 end
0361
0362 fwrite(fid, data(:), prec);
0363 fclose(fid);
0364 return;
0365
0366
0367
0368
0369
0370
0371 function [new_ext_info] = inner_update_extra_ch_info(meg_info, ch_info)
0372
0373 ext_info = meg_info.ExtraChannelInfo;
0374
0375 n_ch_org = size(ext_info.Channel_active, 1);
0376 n_ch_ext = size(ch_info.Active, 1);
0377
0378
0379 if ~isempty(ext_info.Channel_id)
0380 last_ch_id_org = max(ext_info.Channel_id);
0381 elseif ~isempty(meg_info.MEGch_id)
0382 last_ch_id_org = max(meg_info.MEGch_id);
0383 else
0384 last_ch_id_org = 0;
0385 end
0386 ch_info.ID = [];
0387
0388 next_ch_id = last_ch_id_org + 1;
0389 for i_ch = 1:n_ch_ext
0390 ch_info.ID = [ch_info.ID; next_ch_id];
0391 next_ch_id = next_ch_id + 1;
0392 end
0393
0394 ext_info.Channel_active = [ext_info.Channel_active; ch_info.Active];
0395 ext_info.Channel_type = [ext_info.Channel_type; ch_info.Type];
0396 ext_info.Channel_id = [ext_info.Channel_id; ch_info.ID];
0397
0398 for i_ch = 1:n_ch_ext
0399 ext_info.Channel_name{n_ch_org+i_ch} = ch_info.Name{i_ch};
0400 end
0401
0402 if n_ch_org == 0
0403 ext_info.Channel_name = ext_info.Channel_name';
0404 end
0405
0406 new_ext_info = ext_info;
0407 return;
0408
0409
0410
0411
0412
0413