Home > NoiseTools > nt_read_data.m

nt_read_data

PURPOSE ^

[p,data]=nt_read_data(fname,flag) - read data from file

SYNOPSIS ^

function [p,data]=nt_read_data(fname,flag)

DESCRIPTION ^

[p,data]=nt_read_data(fname,flag) - read data from file

 
  fname: file to read
  flag: specify how to deal with complex data
       0: walk through and select 
       1: choose first of each [default]
       2: return all as struct

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [p,data]=nt_read_data(fname,flag)
0002 %[p,data]=nt_read_data(fname,flag) - read data from file
0003 %
0004 %
0005 %  fname: file to read
0006 %  flag: specify how to deal with complex data
0007 %       0: walk through and select
0008 %       1: choose first of each [default]
0009 %       2: return all as struct
0010 %
0011 %
0012 VERBOSE=1;
0013 
0014 if nargin < 1 ; error('!'); end
0015 if nargin < 2; flag=1; end
0016 
0017 if ~isa(fname, 'char'); 
0018     error('filename is not char string');
0019 end
0020 if exist(fname,'file')~=2 
0021     error(['file >', fname, '< not found']);
0022 end
0023 
0024 % standard fields
0025 p.fname=fname;
0026 p.read_with=[];
0027 p.sr=[];
0028 
0029 % intercept directories
0030 if exist(fname,'dir') 
0031     dname=fname; clear fname;
0032     if VERBOSE; disp('directory'); end    
0033     d=dir(dname);
0034     fnames=char(d.name);
0035     idx=find(fnames(:,1)~='.');  % remove '.' and '..' and invisible files
0036     d=d(idx);
0037     fnames=fnames(idx,:);
0038     if numel(d)==0
0039         error(['directory >',fname,'< is empty']);
0040     end
0041     
0042     
0043     % separate directories and files
0044     didx=find([d.isdir]);
0045     fidx=find(~[d.isdir]);
0046     fnames=fnames([didx, fidx],:);
0047     
0048     switch flag
0049         case 2
0050             % return all as array
0051             data={}; p={};
0052             for iFile=1:size(fnames,1);
0053                 [pp,dd]=nt_read_data([dname,deblank(fnames(iFile,:))],flag);
0054                 p{iFile}=pp;
0055                 data{iFile}=dd;
0056             end
0057             return
0058         case 1
0059             % return first
0060             if VERBOSE; disp(['choosing file: ', fnames(1,:)]); end
0061             [p,data]=nt_read_data([dname,deblank(fnames(1,:))],flag);
0062             return
0063         case 0
0064             % choose one
0065             ;
0066     end
0067     
0068    % count files within the directories
0069     nfiles=zeros(numel(didx),1);
0070     for k=1:numel(didx)
0071         dd=dir([data,'/',d(didx(k)).name]);
0072         fns=char(dd.name);
0073         idx=find(fns(:,1)~='.');  % remove '.' and '..' and invisible files
0074         nfiles(k)=numel(idx);
0075     end
0076     
0077     % size of the files
0078     mbytes=[d(fidx).bytes]'/1024;
0079    
0080     % string arrays to put in dialog list
0081     a=repmat(' (', numel(d),1);
0082     if numel(didx)>0
0083         b=cellstr(num2str(nfiles, '%9d'));
0084     else
0085         b=[]; % stupid matlab!
0086     end
0087     if numel(fidx)>0
0088         b=[b;cellstr(num2str(mbytes,'%0.1f'))];
0089     end
0090     b=char(b);
0091     c=[repmat(' files)', numel(didx),1); repmat(' Mb)   ', numel(fidx),1)];
0092      
0093     % which directory or file is user interested in?
0094     i=listdlg('liststring',cellstr([fnames,a,b,c]),...
0095         'name', 'Select file:', ...
0096         'listsize', [300 300], ...
0097         'OKstring','Select',...
0098         'PromptString','choose file');
0099     
0100     if numel(i)==1; 
0101         [p,data]=nt_read_data([dname,deblank(fnames(i,:))]); 
0102     elseif isempty(i)
0103         p=[]; data=[];
0104     else
0105         % load a bunch of files
0106         p.fnames=fnames(i);
0107         for iFile=1:numel(i);
0108             [pp,dd]=nt_read_data([dname,deblank(fnames(i(iFile),:))]);
0109             p.p{iFile}=pp;
0110             data{iFile}=dd;
0111         end
0112     end
0113     return
0114  
0115 end
0116 
0117 % intercept .mat files
0118 if numel(fname)>4 & fname(end-3:end)=='.mat'
0119     p.read_with='.mat file';
0120     if VERBOSE; disp('mat file'); end
0121     % list variables in file, ask user to choose one
0122     switch flag
0123         case 2
0124             % load all as struct
0125             data=load(fname);
0126             return
0127         case 1
0128             % load first variable in mat file
0129             S=whos('-file',fname);
0130             if VERBOSE; disp(['choosing variable: ',S(1).name]); end
0131             data=load(fname,deblank(S(1).name));
0132             while isstruct(data);
0133                 % load first field in structure
0134                 if numel(data)>1
0135                     data=data(1); 
0136                 end
0137                 S=fieldnames(data);
0138                 if VERBOSE; disp (['choosing field: ',S{1}]); end
0139                 data=getfield(data,S{1});
0140             end
0141             return
0142         case 0
0143             % choose
0144     end
0145     S=whos('-file',fname);
0146     var_names=char(S.name);
0147     var_sizes=round([S.bytes]/1024)';
0148     a=repmat(' (', size(var_names,1),1);
0149     b=cellstr(num2str(var_sizes, '%9d'));
0150     b=char(b);
0151     c=[repmat(' Mb)', size(var_names,1),1)];
0152     i=listdlg('liststring',cellstr([var_names,a,b,c]),...
0153         'name', ['Select variable in file ',fname], ...
0154         'listsize', [600 300], ...
0155         'OKstring','Select',...
0156         'PromptString','select:');
0157     if isempty(i); data=[]; return; end
0158     if nargout>1;
0159         data=load(fname,deblank(var_names(i,:)));
0160         % if it's a structure, list fields, ask user to choose one
0161         while isstruct(data);
0162             if numel(data)>1
0163                 i=listdlg('liststring',cellstr(S),...
0164                     'name', ['Select element of strucure array ',var_names(i,:)], ...
0165                     'listsize', [600 300], ...
0166                     'OKstring','Select',...
0167                     'PromptString','select:');
0168                 if i ; data=data(i); end
0169             end
0170             S=fieldnames(data);
0171             i=listdlg('liststring',cellstr(S),...
0172                 'name', ['Select field in struct ',var_names(i,:)], ...
0173                 'listsize', [600 300], ...
0174                 'OKstring','Select',...
0175                 'PromptString','select:');
0176             if i ; data=getfield(data,S{i}); end
0177         end
0178     end
0179     return
0180 end
0181 
0182 % intercept Yokogawa files
0183 if numel(fname)>4 & (fname(end-3:end)=='.con' | fname(end-3:end)=='.sqd')
0184     p.read_with='yokogawa 2013';
0185     p.acq_cond = getYkgwHdrAcqCond(fname);
0186     p.channel_info=getYkgwHdrChannel(fname);
0187     p.system_info=getYkgwHdrSystem(fname);
0188     p.event=getYkgwHdrEvent(fname);
0189     % read other info?
0190     p.sr=p.acq_cond.sample_rate;
0191     if nargout>1;
0192         data=getYkgwData(fname)';
0193     end
0194     return
0195 end
0196    
0197          
0198 % select file reader among those available
0199 has_ft_reader=0; 
0200 has_sopen=0;
0201 if 2==exist('ft_read_header');
0202     has_ft_read_header=1;
0203 else
0204     warning('function ft_read_header() not found: download FieldTrip and/or adjust path');
0205 end
0206 if 2==exist('sopen');
0207     has_sopen=1;
0208 else
0209     warning('function sopen() not found: download BIOSIG and/or adjust path');
0210 end
0211     
0212     
0213 if has_ft_read_header
0214     isftReadable=0;
0215     try
0216         % readable by FieldTrip?
0217         h=ft_read_header(fname);
0218         isftReadable=1;
0219     catch
0220         ; % can't read
0221     end
0222 end
0223 if ~isftReadable & has_sopen
0224     isBiosigReadable=0;
0225     try
0226         % readable by biosig?
0227         h=sopen(fname);
0228         isBiosigReadable=1;
0229         sclose(h);
0230     catch
0231         ; % can't read
0232     end
0233 end
0234     
0235 if isftReadable
0236     if VERBOSE; disp('read with FieldTrip'); end
0237     h=ft_read_header(fname);    
0238     p.header=h;
0239     p.read_with='FieldTrip';
0240     p.sr=h.Fs;
0241     if nargout>1;
0242         data=ft_read_data(fname)';
0243     end
0244 elseif isBiosigReadable
0245     if VERBOSE; disp('read with Biosig'); end
0246     h=sopen(fname);
0247     p.header=h;
0248     p.read_with='BIOSIG';
0249     p.sr=h.SampleRate;
0250     if nargout>1;
0251         data=sread(h)';
0252     end
0253     sclose(h);
0254 else
0255     ismatfile=0;
0256     try
0257         % .mat file?
0258         S=whos('-file',data);
0259         if numel(S)>1
0260             if nargout==2
0261                 [p,data]=nt_read_data([fname,'.mat']);
0262             else
0263                 [p,data]=nt_read_data([fname,'.mat']);
0264             end
0265         end
0266     catch
0267         disp(['File >',fname,'< is not a matlab file, and FieldTrip and BIOSIG can''t read it']);
0268     end
0269 end
0270     
0271

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005