1 function [HDR,data,t]=
matread(HDR,arg2,idxlist)
2 % MATRREAD Loads (parts of) data stored in Matlab-format
4 % [HDR,data,timeindex]=
matread(HDR,block_number, [startidx, endidx])
5 % This is the recommended use
for Matlab-files generated from ADICHT data
6 % Before
using MATREAD, HDR=MATOPEN(filename,
'ADI', ...) must be applied.
8 % [HDR,data,timeindex]=
matread(HDR,Variable_Name, [startidx, endidx])
9 % can be used for other Matlab4 files.
10 % Variable name is a
string which identifies a Matlab Variable.
11 % Before using MATREAD, HDR=MATOPEN(filename, 'r', ...) must be applied.
13 % see also: EEGREAD, FREAD, EEGOPEN, EEGCLOSE
16 % $Id:
matread.m 2205 2009-10-27 12:18:15Z schloegl $
17 % Copyright (c) 1997-2003 by Alois Schloegl
20 % This library is free software; you can redistribute it and/or
21 % modify it under the terms of the GNU Library General Public
22 % License as published by the Free Software Foundation; either
23 % Version 2 of the License, or (at your option) any later version.
25 % This library is distributed in the hope that it will be useful,
26 % but WITHOUT ANY WARRANTY; without even the implied warranty of
27 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 % Library General Public License for more details.
30 % You should have received a copy of the GNU Library General Public
31 % License along with this library; if not, write to the
32 % Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33 % Boston, MA 02111-1307, USA.
37 if strcmp(HDR.TYPE,'ADI'); %HDR.ADI.Mode,
39 if isempty(BlockNo), BlockNo=1:length(HDR.ADI.DB); end;
40 if (isinf(BlockNo) | (length(BlockNo)~=1))
42 fprintf(2,'Warning MATREAD: missing output argument, time information cannot be returned\n');
44 CHd=HDR.ADI.DB(BlockNo);
45 CHt=HDR.ADI.TB(BlockNo);
46 elseif ((BlockNo<1) | BlockNo>length(HDR.ADI.DB)),
47 fprintf(2,'Warning MATREAD: tried to read block %i, but only %i blocks in file %s\n',BlockNo,length(HDR.ADI.DB),HDR.FileName);
50 CHd=HDR.ADI.DB(BlockNo);
51 CHt=HDR.ADI.TB(BlockNo);
55 fprintf(2,'Warning MATREAD: Variable identified by number\n');
58 CHd=find(strcmp(arg2,{HDR.Var.Name}));
61 fprintf(2,
'Warning MATREAD: Variable %s not found\n',arg2);
67 fprintf(2,
'Warning MATREAD: Only one block/variable can be read\n');
74 idxlist=[1,HDR.Var(CHd).Size(2)];
76 if ~all(isfinite(idxlist)),
77 idxlist=[1,HDR.Var(CHd).Size(2)];
81 %
if length(idxlist)<2,idxlist=idxlist*[1 1]; end;
83 if (idxlist(1)<1) %| (idxlist(length(idxlist))>HDR.Var(CHd).Size(2))),
84 fprintf(2,
'Warning MATREAD #%i: Invalid File Position %f-%f\n',CHd,[idxlist(1)-1,idxlist(length(idxlist))]);
88 if idxlist(length(idxlist))>HDR.Var(CHd).Size(2),
89 fprintf(2,
'Warning MATREAD #%i: endidx exceeds block length %f-%f\n',CHd,[HDR.Var(CHd).Size(2),idxlist(length(idxlist))]);
90 idxlist=[idxlist(1),HDR.Var(CHd).Size(2)];
93 if 0; %((idxlist(1)<1) | (idxlist(length(idxlist))>HDR.Var(CHd).Size(2))),
94 fprintf(2,
'ERROR MATREAD #%i: Invalid File Position %f-%f\n',CHd,[idxlist(1)-1,idxlist(length(idxlist))]);
97 Pos = (idxlist(1)-1) * HDR.Var(CHd).Size(1) * HDR.Var(CHd).SizeOfType;
98 Len = min(idxlist(length(idxlist)), HDR.Var(CHd).Size(2)) - idxlist(1) + 1;
100 fseek(HDR.FILE.FID, round(HDR.Var(CHd).Pos + Pos), -1); % round must be explicite, otherwise fseek does incorrect rounding
101 [msg,errno] = ferror(HDR.FILE.FID);
102 if errno, fprintf(2,
'ERROR MATREAD: FSEEK does not work Pos=%f\n%s',HDR.Var(CHd).Pos+Pos,msg);
return; end;
106 if ~any(CHd==HDR.ADI.DB), %
for non-data blocks
107 data=repmat(nan,HDR.Var(CHd).Size(1),Len);
109 cc = min(2^12,Len-count);
110 % [CHd,idxlist(1),idxlist(length(idxlist)),Len,HDR.Var(CHd).Size(2)]
112 data(:,count+(1:size(dta,2))) = dta;
116 BLOCKSIZE=64*3*25*4; % 2^12; % You can change
this for optimizing on your platform
118 %HDR=mat_setfilter(HDR,BlockNo);% set filters
for this block
119 for k=1:max(HDR.SIE.InChanSelect),
122 [tmp,HDR.Filter.Z(:,k)] = filter(HDR.Filter.B,HDR.Filter.A,zeros(length(HDR.Filter.B),1));
123 HDR.FilterOVG.Z = HDR.Filter.Z;
128 if isfield(HDR,
'iFs') & (HDR.iFs>0) & (HDR.iFs<inf),
129 Fs=HDR.SampleRate(find(CHd==HDR.ADI.DB))/HDR.iFs;
130 data = repmat(nan,ceil(Len*size(HDR.SIE.T,2)/size(HDR.SIE.T,1)/Fs),length(HDR.SIE.ChanSelect));
133 data = repmat(nan,ceil(Len*size(HDR.SIE.T,2)/size(HDR.SIE.T,1)),length(HDR.SIE.ChanSelect));
137 cc = min(BLOCKSIZE,Len-count2);
138 % [Len, count2, count, Fs, (Len-count2)*Fs, cc]
139 % [CHd,idxlist(1),idxlist(length(idxlist)),Len,HDR.Var(CHd).Size(2)]
141 % ferror(HDR.FILE.FID)
142 % [Len,cc,count,BLOCKSIZE,HDR.FILE.Pos,ftell(HDR.FILE.FID)],
145 %
if any postprocessing, resample to
internal Sampling rate iFs=1000Hz)
147 dta=
rs(dta
',Fs,1)'; %**************************************************************************************1
150 if nc~=HDR.NS, % reorganizing of channels
151 dta=sparse(find(HDR.ADI.index{arg2}),1:sum(HDR.ADI.index{arg2}>0),1)*dta;
152 dta(find(~HDR.ADI.index{arg2}),:)=nan;
155 for k = sort(HDR.SIE.InChanSelect), %1:size(data,2);
156 %k=HDR.SIE.chanselect(K),
159 [dta(k,:),HDR.Filter.Z(:,k)]=filter(HDR.Filter.B,HDR.Filter.A,dta(k,:),HDR.Filter.Z(:,k));
165 dta =
rs(dta(HDR.SIE.ChanSelect,:)
',HDR.SIE.T); %RS% ***********************************************2
167 dta = dta(HDR.SIE.ChanSelect,:)';
169 data(count+(1:size(dta,1)),:) = dta;
170 count = count + size(dta,1);
171 count2 = count2 + cc;
174 %%%%% Overflow Detection %%%%%
176 for k=1:find(~isnan(sum(HDR.SIE.THRESHOLD,2))),
177 ch = phrchan(HDR.FILE.Name);
178 tmp = (data(:,k)<HDR.SIE.THRESHOLD(1,k)) | (data(:,ch)>HDR.SIE.THRESHOLD(2,k));
186 fseek(HDR.FILE.FID, round(HDR.Var(CHt).Pos + (idxlist(1)-1)*HDR.Var(CHt).SizeOfType),-1);
187 [t,c]=
readnextblock4(HDR.FILE.FID,HDR.Var(CHt),idxlist(length(idxlist))-min(idxlist)+1);
190 if any(abs(tmp-tmp(length(tmp)))>1000*eps)
191 fprintf(2,
'Warning %s: Sampling is not equally spaced in %s [%i,%i]\n',mfilename,HDR.FileName,idxlist(1),idxlist(length(idxlist)));
195 %%%%% Delay of Filtering
197 t = t
' + HDR.Filter.Delay;
202 %
if any postprocessing, resample to
internal Sampling rate iFs=1000Hz)
203 if isfield(HDR,
'iFs') & (HDR.iFs>0) & (HDR.iFs<inf),
204 Fs=HDR.SampleRate(find(CHd==HDR.ADI.DB))/HDR.iFs;
206 t=
rs(t,Fs,1); %*********************************************************************3
210 %%%%% Resampling of the time
212 t=
rs(t,HDR.SIE.T); %RS% %******************************************************************4
215 fprintf(2,
'Warning %s: timeindex is not available \n',mfilename);
224 if dt==0, type =
'float64';
225 elseif dt==6, type =
'uint8';
226 elseif dt==4, type =
'uint16';
227 elseif dt==3, type =
'int16';
228 elseif dt==2, type =
'int32';
229 elseif dt==1, type =
'float32';
231 fprintf(2,
'Error %s: unknown data type\n',mfilename);
235 [data,c]=fread(fid,[VarInfo.Size(1),Len],type);
239 fprintf(2,
'Warning %s: imaginary data not test\n',mfilename);
241 [di,c]=fread(fid,[VarInfo.Size(1),Len],type);
245 if VarInfo.Type(4)==1,data=char(data); end;