0001 function [obj, ch_data, from, to, ch_name, ch_active] = vb_continuous_file_get_sample(obj, ch_list, from, to)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
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
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
0080 MARGIN = ceil(freq * 10);
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
0094 if ~isempty(obj.read_ch_list)
0095 load_spec.ChannelName = obj.read_ch_list;
0096 load_spec.ActiveChannel = false;
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
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
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
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 if nargout < 1
0158 error('function caller should receive return values');
0159 end
0160
0161