Home > vbmeg > functions > gui > preAnalysis > meeg_processor > utility > private_func > vb_continuous_file_get_sample.m

vb_continuous_file_get_sample

PURPOSE ^

return specified channel data.

SYNOPSIS ^

function [obj, ch_data, from, to, ch_name, ch_active] = vb_continuous_file_get_sample(obj, ch_list, from, to)

DESCRIPTION ^

 return specified channel data.
 [USAGE]
    [obj, ch_data] = vb_continuous_file_get_sample(obj, ch_list, from, to);
 [IN]
        obj : continuous file object.
    ch_list : channel name list to get data {Nx1}
       from : get from this sample number.
         to : get data 'from' to 'to' .
 [OUT]
        obj : continuous file object.
    ch_data : specified channel data
       from : got from this sample number.
         to : got data until this sample number.
    ch_name : read channel name
  ch_active : flag list of channel are active or not.
 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:

SOURCE CODE ^

0001 function [obj, ch_data, from, to, ch_name, ch_active] = vb_continuous_file_get_sample(obj, ch_list, from, to)
0002 % return specified channel data.
0003 % [USAGE]
0004 %    [obj, ch_data] = vb_continuous_file_get_sample(obj, ch_list, from, to);
0005 % [IN]
0006 %        obj : continuous file object.
0007 %    ch_list : channel name list to get data {Nx1}
0008 %       from : get from this sample number.
0009 %         to : get data 'from' to 'to' .
0010 % [OUT]
0011 %        obj : continuous file object.
0012 %    ch_data : specified channel data
0013 %       from : got from this sample number.
0014 %         to : got data until this sample number.
0015 %    ch_name : read channel name
0016 %  ch_active : flag list of channel are active or not.
0017 % Copyright (C) 2011, ATR All Rights Reserved.
0018 % License : New BSD License(see VBMEG_LICENSE.txt)
0019 
0020 %
0021 % --- Previous check
0022 %
0023 if ~exist('obj', 'var')
0024     error('obj is a required parameter.');
0025 end
0026 if ischar(ch_list)
0027     ch_list = {ch_list};
0028 end
0029 if isempty(ch_list)
0030     ch_list = cell(0);
0031 end
0032 if ~iscellstr(ch_list)
0033     error('ch_list should be cell string.');
0034 end
0035 if ~exist('from', 'var')
0036     error('from is a required parameter.');
0037 end
0038 if ~exist('to', 'var')
0039     error('to is a required parameter.');
0040 end
0041 
0042 if ~isfield(obj, 'cache_data')
0043     obj.cache_data = struct;
0044 end
0045 if ~isfield(obj.cache_data, 'sample_list')
0046     obj.cache_data.sample_list = [];
0047 end
0048 if ~isfield(obj.cache_data, 'data')
0049     obj.cache_data.data = [];
0050 end
0051 if ~isfield(obj.cache_data, 'Name')
0052     obj.cache_data.Name = [];
0053 end
0054 
0055 %
0056 % --- Main Procedure
0057 %
0058 Nsample = vb_continuous_file_get_Nsample(obj);
0059 if from <= 0
0060     from = 1;
0061 end
0062 if to > Nsample
0063     to = Nsample;
0064 end
0065 
0066 freq = vb_continuous_file_get_sample_freq(obj);
0067 
0068 
0069 cix_from = [];
0070 cix_to   = [];
0071 ch_name  = [];
0072 
0073 if ~isempty(obj.cache_data.sample_list)
0074     cix_from = find(obj.cache_data.sample_list == from);
0075     cix_to   = find(obj.cache_data.sample_list == to);
0076 end
0077 
0078 if isempty(cix_from) || isempty(cix_to)
0079     % miss hit
0080     MARGIN = ceil(freq * 10); % margin pre-post data
0081     if from-MARGIN < 0
0082         read_from = 1;
0083     else
0084         read_from = from-MARGIN;
0085     end
0086     if to + MARGIN > Nsample
0087         read_to = Nsample;
0088     else
0089         read_to = to + MARGIN;
0090     end
0091     read_len = read_to - read_from + 1;
0092 
0093     % load data
0094     if ~isempty(obj.read_ch_list)
0095         load_spec.ChannelName = obj.read_ch_list;
0096         load_spec.ActiveChannel = false; % all the channels including inactive
0097         load_spec.Pretrigger  = 0;
0098         load_spec.Trigger     = read_from;
0099         load_spec.Posttrigger = read_to - read_from;
0100         warning('OFF', 'MATLAB:hg:uicontrol:ParameterValuesMustBeValid');
0101         h = msgbox('Now reading data...', 'Please wait');
0102         bh = findall(h, 'Style', 'pushbutton');
0103         set(bh, 'Visible', 'off', 'Enable', 'off');
0104         drawnow; pause(0.1);
0105         warning('ON', 'MATLAB:hg:uicontrol:ParameterValuesMustBeValid');
0106 
0107         [obj.cache_data.data, ch_info] = vb_load_meg_data(obj.filename, load_spec);
0108         if isfield(ch_info, 'Name')
0109             obj.cache_data.Name    = ch_info.Name;
0110             obj.cache_data.hashkey = struct;
0111             obj.cache_data.active  = ch_info.Active;
0112             for j=1:length(ch_info.Name)
0113                 % fieldname is used as a hash key for speeding
0114                 hashkey = ['hash_' vb_CalcMD5(ch_info.Name{j}, 'char')];
0115                 obj.cache_data.hashkey.(hashkey) = j;
0116             end
0117         end
0118         if ishandle(h), delete(h); end
0119         obj.cache_data.sample_list = [read_from:read_to];
0120     end
0121 end
0122 
0123 data_ix = [];
0124 for k=1:length(ch_list)
0125     hashkey = ['hash_' vb_CalcMD5(ch_list{k}, 'char')];
0126     if isfield(obj.cache_data.hashkey, hashkey)
0127         data_ix = [data_ix; obj.cache_data.hashkey.(hashkey)];
0128     end
0129 end
0130 % return data
0131 cix_from = find(obj.cache_data.sample_list == from);
0132 cix_to   = find(obj.cache_data.sample_list == to);
0133 if ~isempty(obj.cache_data.data)
0134     ch_data   = obj.cache_data.data(data_ix, cix_from:cix_to);
0135     ch_name   = obj.cache_data.Name(data_ix);
0136     ch_active = obj.cache_data.active(data_ix);
0137 else
0138     ch_data   = [];
0139     ch_name   = [];
0140     ch_active = [];
0141 end
0142 
0143 from = obj.cache_data.sample_list(cix_from);
0144 to   = obj.cache_data.sample_list(cix_to);
0145 
0146 % % debug
0147 % if size(obj.cache_data.sample_list, 2) ~= 1787500
0148 %     dbstack;
0149 %     a = 1;
0150 % end
0151 
0152 
0153 
0154 %
0155 % --- After check
0156 %
0157 if nargout < 1
0158     error('function caller should receive return values');
0159 end
0160 
0161

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