Replace data and update MEG-MAT file. This function is used to create/update MEG-MAT file with arbitrary signal preprocessing function not supported by VBMEG. [syntax] vb_megfile_update_data(inst_spec) [input] inst_spec : <<required>> <struct> instruction spec : fields are as follows: : .org_file : <<required>> MEG-MAT file which will be updated : .new_data : <<required>> [n_channel x n_sample] : .channel_type : <<optional>> ['MEG'] | 'AXIAL' | 'PLANAR' | 'EXTRA' : .new_file : <<optional>> if you want to make new file. : : ['org_file'] : : Empty means overwriting '.org_file'. : .bin_data_dir : <<optional>> data stored directory. : : This parameter is valid when '.new_file' is valid. : : [(body of 'new_file')_bin] : : e.g. './new_0123.meg.mat' --> './new_0123_bin' [output] void [note] The size of inst_spec.new_data must be the same as the original one. [example] 1 File create mode. >> bexp = vb_load_meg_data('meg_file_org.meg.mat'); >> bexp_new = preprocess_fnc(bexp); % s.t. size(bexp)==size(bexp_new) >> inst_spec.org_file = 'meg_file_org.meg.mat'; >> inst_spec.new_data = bexp_new; >> inst_spec.new_file = 'meg_file_new.meg.mat'; >> inst_spec.bin_data_dir = 'meg_file_new'; % relative path >> vb_megfile_update_data(inst_spec); 2 File update mode. >> bexp = vb_load_meg_data('meg_file_org.meg.mat'); >> bexp_new = preprocess_fnc(bexp); % s.t. size(bexp)==size(bexp_new) >> inst_spec.org_file = 'meg_file_org.meg.mat'; >> inst_spec.new_data = bexp_new; >> vb_megfile_update_data(inst_spec); 3 File update mode with specified ('AXIAL' here) channel type. >> [bexp,channel_info] = vb_load_meg_data('meg_file_org.meg.mat'); >> ix = find(channel_info.Type==2); % 2 is Type of 'AXIAL' sensor. % This is valid for Yokogawa MEG. >> bexp_axial = bexp(ix,:); % get 'AXIAL' sensor data >> bexp_axial_new = preprocess_fnc(bexp_axial); >> inst_spec.org_file = 'meg_file_org.meg.mat'; >> inst_spec.new_data = bexp_axial_new; % Only 'AXIAL' data updated >> inst_spec.channel_type = 'AXIAL'; >> vb_megfile_update_data(inst_spec) [history] 2010-11-22 (Sako) trial version Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function vb_megfile_update_data(inst_spec) 0002 % Replace data and update MEG-MAT file. 0003 % 0004 % This function is used to create/update MEG-MAT file with arbitrary 0005 % signal preprocessing function not supported by VBMEG. 0006 % 0007 % [syntax] 0008 % vb_megfile_update_data(inst_spec) 0009 % 0010 % [input] 0011 % inst_spec : <<required>> <struct> instruction spec 0012 % : fields are as follows: 0013 % : .org_file : <<required>> MEG-MAT file which will be updated 0014 % : .new_data : <<required>> [n_channel x n_sample] 0015 % : .channel_type : <<optional>> ['MEG'] | 'AXIAL' | 'PLANAR' | 'EXTRA' 0016 % : .new_file : <<optional>> if you want to make new file. 0017 % : : ['org_file'] 0018 % : : Empty means overwriting '.org_file'. 0019 % : .bin_data_dir : <<optional>> data stored directory. 0020 % : : This parameter is valid when '.new_file' is valid. 0021 % : : [(body of 'new_file')_bin] 0022 % : : e.g. './new_0123.meg.mat' --> './new_0123_bin' 0023 % 0024 % [output] 0025 % void 0026 % 0027 % [note] 0028 % The size of inst_spec.new_data must be the same as the original one. 0029 % 0030 % [example] 0031 % 1 File create mode. 0032 % >> bexp = vb_load_meg_data('meg_file_org.meg.mat'); 0033 % >> bexp_new = preprocess_fnc(bexp); % s.t. size(bexp)==size(bexp_new) 0034 % >> inst_spec.org_file = 'meg_file_org.meg.mat'; 0035 % >> inst_spec.new_data = bexp_new; 0036 % >> inst_spec.new_file = 'meg_file_new.meg.mat'; 0037 % >> inst_spec.bin_data_dir = 'meg_file_new'; % relative path 0038 % >> vb_megfile_update_data(inst_spec); 0039 % 0040 % 2 File update mode. 0041 % >> bexp = vb_load_meg_data('meg_file_org.meg.mat'); 0042 % >> bexp_new = preprocess_fnc(bexp); % s.t. size(bexp)==size(bexp_new) 0043 % >> inst_spec.org_file = 'meg_file_org.meg.mat'; 0044 % >> inst_spec.new_data = bexp_new; 0045 % >> vb_megfile_update_data(inst_spec); 0046 % 0047 % 3 File update mode with specified ('AXIAL' here) channel type. 0048 % >> [bexp,channel_info] = vb_load_meg_data('meg_file_org.meg.mat'); 0049 % >> ix = find(channel_info.Type==2); % 2 is Type of 'AXIAL' sensor. 0050 % % This is valid for Yokogawa MEG. 0051 % >> bexp_axial = bexp(ix,:); % get 'AXIAL' sensor data 0052 % >> bexp_axial_new = preprocess_fnc(bexp_axial); 0053 % >> inst_spec.org_file = 'meg_file_org.meg.mat'; 0054 % >> inst_spec.new_data = bexp_axial_new; % Only 'AXIAL' data updated 0055 % >> inst_spec.channel_type = 'AXIAL'; 0056 % >> vb_megfile_update_data(inst_spec) 0057 % 0058 % [history] 0059 % 2010-11-22 (Sako) trial version 0060 % 0061 % Copyright (C) 2011, ATR All Rights Reserved. 0062 % License : New BSD License(see VBMEG_LICENSE.txt) 0063 0064 % --- CHECK ARGUMENTS --- % 0065 if ~exist('inst_spec', 'var'), inst_spec = []; end 0066 [inst_spec] = inner_check_arguments(inst_spec); 0067 0068 % --- MAIN PROCEDURE --------------------------------------------------------- % 0069 % 0070 meg_file = inst_spec.org_file; 0071 [ch_info] = vb_load_channel_info(meg_file, inst_spec.channel_type); 0072 0073 % --- check data is storing inside or outside 0074 [state_bexp, const] = vb_util_check_variable_in_matfile(meg_file, 'bexp'); 0075 0076 if state_bexp ~= const.VALID 0077 inner_update_external_data(inst_spec, ch_info); 0078 else 0079 inner_update_internal_data(inst_spec, ch_info); 0080 end 0081 0082 % 0083 % --- END OF MAIN PROCEDURE -------------------------------------------------- % 0084 0085 % --- INNER FUNCTIONS -------------------------------------------------------- % 0086 % 0087 % --- inner_check_arguments() 0088 % 0089 function [inst_spec] = inner_check_arguments(inst_spec) 0090 if isempty(inst_spec) 0091 error('(%s) inst_spec is a required parameter', mfilename); 0092 end 0093 0094 if ~isfield(inst_spec, 'org_file') || isempty(inst_spec.org_file) 0095 error('(%s) inst_spec.org_file is a required field', mfilename); 0096 end 0097 0098 if exist(inst_spec.org_file, 'file') ~= 2 0099 error('(%s) cannot find inst_spec.org_file : %s', ... 0100 mfilename, inst_spec.org_file); 0101 end 0102 0103 if ~isfield(inst_spec, 'new_data') || isempty(inst_spec.new_data) 0104 error('(%s) inst_spec.new_data is a required field', mfilename); 0105 end 0106 0107 if ~isfield(inst_spec, 'channel_type') || isempty(inst_spec.channel_type) 0108 inst_spec.channel_type = 'MEG'; 0109 end 0110 0111 if ~isfield(inst_spec, 'new_file') || isempty(inst_spec.new_file) 0112 inst_spec.new_file = ''; 0113 0114 elseif ~isfield(inst_spec, 'bin_data_dir') || isempty(inst_spec.bin_data_dir) 0115 [fpath, fname] = vb_get_file_parts(inst_spec.new_file); 0116 idx = strfind(fname, '.')-1; 0117 inst_spec.bin_data_dir = [fname(1:idx) '_bin']; 0118 end 0119 % 0120 % --- end of inner_check_arguments() 0121 0122 % --- inner_update_external_data() 0123 % 0124 function inner_update_external_data(inst_spec, ch_info) 0125 func_ = mfilename; 0126 0127 meg_file = inst_spec.org_file; 0128 meg_info = vb_load_measurement_info(meg_file); 0129 0130 if ~isempty(inst_spec.new_file) 0131 % --- copy meg_file - by using vb_load_meg_data() 0132 load_spec.ChannelType = 'ALL'; 0133 load_spec.saveman = meg_info.saveman; 0134 load_spec.saveman.data_dir = inst_spec.bin_data_dir; 0135 vb_load_meg_data(meg_file, load_spec, inst_spec.new_file); 0136 0137 tmp_dir = inst_spec.bin_data_dir; 0138 meg_root = vb_get_file_parts(inst_spec.new_file); 0139 0140 else 0141 % --- overwrite original file 0142 tmp_dir = meg_info.saveman.data_dir; 0143 meg_root = vb_get_file_parts(meg_file); 0144 0145 inst_spec.new_file = meg_file; 0146 end 0147 0148 % ----- bin directory is defined by relative path from megfile directory 0149 data_dir = [meg_root '/' tmp_dir]; 0150 0151 PRECISION = meg_info.saveman.precision; 0152 0153 const = vb_define_yokogawa; 0154 0155 n_channel = size(ch_info.Name, 1); 0156 for i_ch = 1:n_channel 0157 ch_name = ch_info.Name{i_ch}; 0158 cur_ch_file = sprintf('%s/%s.%s', ... 0159 data_dir, ch_name, const.EXT_CHANNEL_BIN); 0160 0161 cur_fid = fopen(cur_ch_file, 'wb'); 0162 if cur_fid == -1 0163 error('(%s)cannot open file (%s)', func_, cur_ch_file); 0164 end 0165 0166 fwrite(cur_fid, inst_spec.new_data(i_ch, :), PRECISION); 0167 fclose(cur_fid); 0168 % fprintf('--- UPDATED %s\n', cur_ch_file); 0169 end 0170 0171 % --- no change .meg.mat file of their own ... 0172 0173 return; 0174 % 0175 % --- end of inner_update_external_data() 0176 0177 % --- inner_update_internal_data() 0178 % 0179 function inner_update_internal_data(inst_spec, ch_info) 0180 meg_file = inst_spec.meg_file; 0181 meg = load(meg_file); 0182 meg.bexp(ch_info.ID, :) = inst_spec.new_data(:,:); 0183 0184 if ~isempty(inst_spec.new_meg_file) 0185 vb_save_struct(inst_spec.new_meg_file, meg); 0186 else 0187 vb_save_struct(meg_file, meg); 0188 end 0189 return; 0190 % 0191 % --- end of inner_update_internal_data() 0192 % 0193 % --- END OF INNER FUNCTIONS ------------------------------------------------- % 0194 0195 % --- END OF FILE --- %