function loadcsv(varargin); %LOADCSV file reading utility M Boldin, WRDS Feb 2003 % Built for comma-delimitted (csv) WRDS output % that may have missing values and character values. % % If the first data file line has no numerics, % it is assumed to be a header line with variable names. % Nonnumerics in subsequent lines are changed to NaN. % Results are put into base memory using filename. % Structure class used if headerline has variable names. % see fname.vnames & fname.data % % LOADCSV (no input args) uses GUI file selection (uigetfiles) % LOADCSV(1) command line version % LOADCSV('file_name','C:\data_path\') works too, path is optional %Setting file selection method parameters; if(nargin==0); path=[]; fname=[]; arg1=0; elseif(varargin{1}==1); arg1=1; else; fname=varargin{1}; arg1=-1; end; if(nargin>1) path=varargin{2}; else; path=[]; end; disp 'Running CSV data file reading utility'; if(arg1==0); % GUI method to select file; [fname, path] = uigetfile( ... { ... '*.txt;*.csv;*.dat', 'ASCII Test Data Files'; ... '*.*','All Files (*.*)' ... },'Pick a file to load/read'); elseif(arg1==1); % Command line method -- select file; fname=input('Input file name (ex. datafile.txt): ','s'); end; %Open file and check to see it exists fullname=[path fname]; [fid, message] = fopen(fullname,'r'); if isempty(fid) | (fid < 0) error(['File: ', fname, ' not found (in the MATLAB Path).']); else, disp(['Opening file: ', fname]) end [fn1 fn2]=strtok(fname,'.'); fext=strrep(fn2,'.',''); fline=''; fline1=fgetl(fid); disp 'First line:'; disp(fline1); fcomma=strfind(fline1,','); if ~isempty(fcomma), disp 'Comma delimitted will be assumed'; dlm=','; [r n] = size(strfind(fline1,dlm)); nv=n+1; else; dlm=[]; disp 'WARNING Comma delimitted data is required for this version to work correctly'; ss1=strread(fline1,'%s'); ss1' nv=prod(size(ss1)); end; disp(['Number of data columns: ' num2str(nv)]); %Count number of numerics in first line; nv1=0; vnames=[' ']; ss1=strread(fline1,'%s',nv,'delimiter',dlm); for jj=1:nv; %vnames(jj)= ( ss1(jj) ); try; x1=str2num(char(ss1(jj))); catch; x1=[]; end; if isnumeric(x1) & ~isempty(x1); nv1=nv1+1; end; %increments one if item jj is numeric; end; %if all numerics, assumed not a header line; if(nv1==nv), header=0; else, header=1; end; if(header==1); disp 'Assumed to be header line and will be skipped for data read'; fline=fgetl(fid); else, fline=fline1; end; %Go through each data file line; xcheck=0; jj=0; readOK=1; while(readOK); jj=jj+1; %disp(fline); try; xcheck=0; xx=strread(fline,'%f',nv,'delimiter',dlm,'emptyvalue',NaN); catch; xx=[]; xcheck=1; %disp(['Problem with line: ' num2str(jj)]); end; [rnv jc]=size(xx); if(rnv < nv), xcheck=1; end; if(~isempty(xx) | xcheck==0); if(prod(size(xx))==nv-1), xx=[xx; NaN]; end; xdat(jj,:)=xx'; else; %disp('Reading for str2num(ber) by column'); xx=strread(fline,'%s',nv,'delimiter',dlm,'emptyvalue',NaN); for cc=1:nv try xxcc=str2num(char(xx(cc))); if ~isempty(xxcc); xdat(jj,cc)=xxcc; else; xdat(jj,cc)=NaN; end; catch xdat(jj,cc)=NaN; end; end; end; fline=fgetl(fid); if(fline==-1); readOK=0; break; end; %if(jj > 10E12), break, end; %limit to rows read; end; [rows columns]=size(xdat); disp(['Rows read & created: ' num2str(rows)]); %Place results into base memory if header==1; xdat=struct('vnames',char(ss1),'data',xdat); assignin('base',fn1,xdat); disp(['Created structure ' fn1 ' with vnames and data fields']); else; assignin('base',fn1,xdat); disp(['Created matrix: ' fn1]); end; disp('***Done***');