Home > vbmeg > functions > device > meg > vb_meginfo_read_data_file.m

vb_meginfo_read_data_file

PURPOSE ^

read data from data file of channel or trial

SYNOPSIS ^

function [data, loadinfo] = vb_meginfo_read_data_file(meginfo, ch_list, sample_list, datatype, tr_list, root_dir)

DESCRIPTION ^

 read data from data file of channel or trial
 [usage]
   [data, loadinfo] = vb_meginfo_read_data_file( ...
     meginfo, ch_list, sample_list, datatype, tr_list, root_dir)
 [input]
    meginfo : <required> <<struct>> MEG information
            :  (required fields are as follows)
            :   .saveman
    ch_list : <optional> list of channel label to read []
            :  e.g. [1 2 3 4]
            :  if this is empty, all channels are read
sample_list : <optional>
            :  sample index array of each trial
            :  [from, to]
            :  if this is empty, all samples are read as single trial
            :  if data type is "continue", [Ntrial x 2]
            :  if data type is "evoked raw", [1 x 2]
   datatype : <optional> data type to read [0]
            :  this is used when ch_list is not specified
            :  if ch_list is valid, this is ignored
            :  0) ALL (MEG + EXTRA)
            :  1) MEG
            :  2) EXTRA
    tr_list : <optional> trial index list [N_trial_new x 1]
            :  if tr_list is valid and this is empty, all trials are selected
   root_dir : <optional> root directory of data file ['.']
            :  normally it is path of megfile
 [output]
       data : read data
            :  [Nchannel x Nsample x Ntrial]
   loadinfo : loading information (undefined yet)
 [note]
   Continuous Data --> be able to be extracted by channel and sample
        Epoch Data --> be able to be extracted by channel and trial
 [history]
   2007-06-12 (Sako) initial version
   2007-08-22 (Sako) added an argument 'root_dir'
   2008-02-19 (Sako) renewed
   2008-04-24 (Sako) numbered Trial(i).number sequentially - continuous data
   2009-07-14 (Sako) supported new format of trial data (each channel)
   2009-07-28 (Sako) supported 'BASIC' format
   2010-07-01 (Sako) modified help regarding 'tr_list'

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data, loadinfo] = vb_meginfo_read_data_file( ...
0002   meginfo, ch_list, sample_list, datatype, tr_list, root_dir)
0003 % read data from data file of channel or trial
0004 % [usage]
0005 %   [data, loadinfo] = vb_meginfo_read_data_file( ...
0006 %     meginfo, ch_list, sample_list, datatype, tr_list, root_dir)
0007 % [input]
0008 %    meginfo : <required> <<struct>> MEG information
0009 %            :  (required fields are as follows)
0010 %            :   .saveman
0011 %    ch_list : <optional> list of channel label to read []
0012 %            :  e.g. [1 2 3 4]
0013 %            :  if this is empty, all channels are read
0014 %sample_list : <optional>
0015 %            :  sample index array of each trial
0016 %            :  [from, to]
0017 %            :  if this is empty, all samples are read as single trial
0018 %            :  if data type is "continue", [Ntrial x 2]
0019 %            :  if data type is "evoked raw", [1 x 2]
0020 %   datatype : <optional> data type to read [0]
0021 %            :  this is used when ch_list is not specified
0022 %            :  if ch_list is valid, this is ignored
0023 %            :  0) ALL (MEG + EXTRA)
0024 %            :  1) MEG
0025 %            :  2) EXTRA
0026 %    tr_list : <optional> trial index list [N_trial_new x 1]
0027 %            :  if tr_list is valid and this is empty, all trials are selected
0028 %   root_dir : <optional> root directory of data file ['.']
0029 %            :  normally it is path of megfile
0030 % [output]
0031 %       data : read data
0032 %            :  [Nchannel x Nsample x Ntrial]
0033 %   loadinfo : loading information (undefined yet)
0034 % [note]
0035 %   Continuous Data --> be able to be extracted by channel and sample
0036 %        Epoch Data --> be able to be extracted by channel and trial
0037 % [history]
0038 %   2007-06-12 (Sako) initial version
0039 %   2007-08-22 (Sako) added an argument 'root_dir'
0040 %   2008-02-19 (Sako) renewed
0041 %   2008-04-24 (Sako) numbered Trial(i).number sequentially - continuous data
0042 %   2009-07-14 (Sako) supported new format of trial data (each channel)
0043 %   2009-07-28 (Sako) supported 'BASIC' format
0044 %   2010-07-01 (Sako) modified help regarding 'tr_list'
0045 %
0046 % Copyright (C) 2011, ATR All Rights Reserved.
0047 % License : New BSD License(see VBMEG_LICENSE.txt)
0048 
0049 % --- CHECK ARGUMENTS --- %
0050 if ~exist('meginfo', 'var'), meginfo = []; end
0051 if ~exist('ch_list', 'var'), ch_list = []; end
0052 if ~exist('sample_list', 'var'), sample_list = []; end
0053 if ~exist('datatype', 'var'), datatype = []; end
0054 if ~exist('tr_list', 'var'), tr_list = []; end
0055 if ~exist('root_dir', 'var'), root_dir = []; end
0056 [meginfo, ch_list, sample_list, tr_list, root_dir] = ...
0057   inner_check_arguments(meginfo, ...
0058     ch_list, sample_list, datatype, tr_list, root_dir);
0059 
0060 % --- MAIN PROCEDURE --------------------------------------------------------- %
0061 %
0062 loadinfo = [];
0063 
0064 n_sample = vb_meginfo_get_sample_number(meginfo);
0065 n_trial  = vb_info_get_Nrepeat(meginfo);
0066 
0067 if n_trial == 1
0068   trial_cond = 'single';
0069   % --- not be extracted by trial
0070   tr_list = [];
0071 else
0072   trial_cond = 'plural';
0073   if size(sample_list, 1) > 1
0074     % --- something wrong
0075     fprintf('(%s) loadspec.Trigger may be wrong\n', mfilename);
0076     sample_list = sample_list(1,:);
0077   end
0078 end
0079 
0080 % --- <<temporary struct>> SAVEINFO --------- %
0081 %   .dir  : directory
0082 %   .prec : precision string like 'float32'
0083 % ------------------------------------------- %
0084 
0085 % changed saveman spec
0086 saveinfo.dir = [root_dir filesep vb_saveman_get_dir(meginfo.saveman)];
0087 saveinfo.trial_cond = trial_cond;
0088 saveinfo.prec = inner_determine_precision(meginfo);
0089 saveinfo.n_trial = n_trial;
0090 
0091 data = inner_read_data_file(saveinfo, n_sample, ch_list, sample_list, tr_list);
0092 % data = inner_read_data_file(saveinfo, n_sample, ch_list, sample_list,
0093 % tr_info);
0094 return;
0095 %
0096 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0097 
0098 
0099 % --- INNER FUNCTIONS -------------------------------------------------------- %
0100 %
0101 % --- inner_check_arguments()
0102 %
0103 function [meginfo, ch_list, sample_list, tr_list, root_dir] = ...
0104   inner_check_arguments(meginfo, ...
0105     ch_list, sample_list, datatype, tr_list, root_dir)
0106 
0107 func_ = mfilename;
0108 
0109 if isempty(meginfo)
0110   error('(%s)meginfo is a required parameter', func_);
0111 end
0112 
0113 if isempty(ch_list)
0114   if isempty(datatype)
0115     datatype = 0;
0116   end
0117 
0118   if datatype == 1
0119     ch_list = vb_meginfo_get_channel_label_meg(meginfo);
0120   elseif datatype == 2
0121     ch_list = vb_meginfo_get_channel_label_extra(meginfo);
0122   else
0123     meg_ch_list = vb_meginfo_get_channel_label_meg(meginfo);
0124     ex_ch_list = vb_meginfo_get_channel_label_extra(meginfo);
0125     ch_list = [meg_ch_list; ex_ch_list];
0126   end
0127 end
0128 
0129 if isempty(sample_list)
0130   sample_list = [1,vb_meginfo_get_sample_number(meginfo)];
0131 end
0132 
0133 if isempty(tr_list)
0134   n_trial = vb_info_get_trial_number(meginfo);
0135   tr_list = (1:n_trial);
0136 end
0137 
0138 if isempty(root_dir)
0139   % default directory is current
0140   root_dir = '.';
0141 end
0142 return;
0143 %
0144 % --- end of inner_check_arguments()
0145 
0146 
0147 % --- inner_read_data_file()
0148 %
0149 % function [loaded_data] = inner_read_data_file( ...
0150 %   saveinfo, n_sample, ch_list, sample_list, tr_info)
0151 function [loaded_data] = inner_read_data_file( ...
0152   saveinfo, n_sample, ch_list, sample_list, tr_list)
0153 
0154 const = vb_define_yokogawa;
0155 func_ = mfilename;
0156 
0157 % decompose SAVEINFO
0158 savedir = saveinfo.dir;
0159 trial_cond = saveinfo.trial_cond;
0160 PRECISION = saveinfo.prec;
0161 n_trial = saveinfo.n_trial;
0162 
0163 n_channel_new = length(ch_list);
0164 
0165 n_sample_new = sample_list(1,2)-sample_list(1,1) + 1;
0166 
0167 switch trial_cond
0168   case 'single'
0169     % --- sample_list [N_new_trial x 2]
0170     n_trial_new = size(sample_list,1);
0171 
0172   case 'plural'
0173     % --- tr_list is a trial number list to extract [N_new_trial x 1]
0174     n_trial_new = length(tr_list);
0175 end
0176 
0177 % --- file extension
0178 ext = const.EXT_CHANNEL_BIN;
0179 
0180 % --- allocate buffer
0181 loaded_data = zeros(n_channel_new, n_sample_new, n_trial_new);
0182 
0183 for i_ch = 1:n_channel_new
0184   ch_label = ch_list{i_ch};
0185     
0186   fpath = sprintf('%s%s%s.%s',savedir, filesep, ch_label, ext);
0187   fid = fopen(fpath, 'rb');
0188   if fid == -1
0189     warning('(%s)cannot open %s', func_, fpath);
0190     continue;
0191   end
0192   
0193   % --- read data
0194   ch_data = fread(fid, inf, PRECISION);
0195   fclose(fid);    
0196 
0197   switch trial_cond
0198     case 'single'
0199       % --- The data which trial size is one like 'Continuous_Raw' can be
0200       %     loaded by channel or sample, but not by trial
0201       %
0202       % --- sample_list is expected [N_new_trial x 2]
0203       % ------- ch_data is expected [n_sample * 1]
0204 
0205       % --- chop data by trial
0206       for i_trial = 1:n_trial_new
0207         loaded_data(i_ch,:,i_trial) = ...
0208           ch_data(sample_list(i_trial,1):sample_list(i_trial,2),:);
0209       end
0210 
0211     case 'plural'
0212       % --- The data which trial size if over 1 like 'Evoked_Raw' can be loaded
0213       %     by channel or trial, but not by sample
0214       % --- Sample_from must be 1 and sample_to must be the length of sample
0215       
0216       % --- ch_data is expected [n_sample * n_trial]
0217       org_ch_data = reshape(ch_data, n_sample, n_trial);
0218       
0219       % --- sample_list expected to be [1 x 2]
0220       sample_from = sample_list(1,1);
0221       sample_to   = sample_list(1,2);
0222       sample_idx = sample_from:sample_to;
0223       loaded_data(i_ch,:,:) = org_ch_data(sample_idx, tr_list);
0224   end
0225 end
0226 return;
0227 %
0228 % --- end of inner_read_data_file()
0229 
0230 
0231 % --- inner_determine_precision(meginfo)
0232 %
0233 function [prec] = inner_determine_precision(meginfo)
0234 func_ = mfilename;
0235 
0236 if isempty(meginfo)
0237   error('(%s)cannot determine precision wihout meginfo', func_);
0238 end
0239 prec = vb_meginfo_get_precision(meginfo);
0240 
0241 if isempty(prec)
0242   % if value is not be set, give default value (defined in vb_define_yokogawa.m)
0243   const = vb_define_yokogawa;
0244   prec = const.STANDARD_BIN_DATA_TYPE;
0245 end
0246 %
0247 % --- end of inner_determine_precision()
0248 %
0249 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0250 
0251 % --- END OF FILE --- %

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005