wolffd@0: function [lc,dflag,dattype]=loadcell(fname,delim,exclusions,options); wolffd@0: %function [lc,dflag,numdata]=loadcell(fname,delim,exclusions); wolffd@0: % wolffd@0: % loadcell loads a cell array with character delimited wolffd@0: % data, which can have variable length lines and content. wolffd@0: % Numeric values are converted from string to double wolffd@0: % unless options is a string containing 'string'. wolffd@0: % wolffd@0: % loadcell is for use with small datasets. It is not optimised wolffd@0: % for large datasets. wolffd@0: % wolffd@0: % fname is the filename to be loaded wolffd@0: % wolffd@0: % delim is/are the relevant delimiter(s). If char(10) is included wolffd@0: % newlines are simply treated as delimiters and a 1-d array is created. wolffd@0: % wolffd@0: % exclusions are the set of characters to be treated as paired wolffd@0: % braces: line ends or delimiters within braces are ignored. wolffd@0: % braces are single characters and any brace can pair with wolffd@0: % any other brace: no type pair checking is done. wolffd@0: % wolffd@0: % options can be omitted or can contain 'string' if no numeric wolffd@0: % conversion is required, 'single' if multiple adjacent seperators wolffd@0: % should not be treated as one, 'free' if all linefeeds should be stripped wolffd@0: % first and 'empty2num' if empty fields are to be treated as numeric wolffd@0: % zeros rather than an empty character set. Combine options using wolffd@0: % concatenation. wolffd@0: % wolffd@0: % lc is a cell array containing the loaded data. wolffd@0: % wolffd@0: % dflag is a set of flags denoting the (i,j) values where data was entered wolffd@0: % dflag(i,j)=1 implies lc(i,j) was loaded from the data, and not just set wolffd@0: % to empty, say, by default. wolffd@0: % wolffd@0: % numdata is an array numdata(i,j)=NaN implies wolffd@0: % lc(i,j) is a string, otherwise it stores the number at i,j. wolffd@0: % This will occur regardless of whether the 'string' option is set. wolffd@0: % wolffd@0: % lc will return -1 if the file is not found or could not be wolffd@0: % opened. wolffd@0: % wolffd@0: % Hint: numdata+(1/dflag-1) provides a concise descriptor for the numeric data wolffd@0: % Inf=not loaded wolffd@0: % NaN=was string or empty set. wolffd@0: % otherwise numeric wolffd@0: % wolffd@0: % EXAMPLE wolffd@0: % wolffd@0: %[a,b]=loadcell('resultsfile',[',' char(9)],'"','single-string'); wolffd@0: % will load file 'resultsfile' into variable a, treating any of tab or wolffd@0: % comma as delimiters. Delimiters or carriage returns lying wolffd@0: % between two double inverted commas will be ignored. Two adjacent delimiters wolffd@0: % will count twice, and all data will be kept as a string. wolffd@0: % wolffd@0: % Note: in space-separated data 'single' would generally be omitted, wolffd@0: % wheras in comma-seperated data it would be included. wolffd@0: % wolffd@0: % Note the exclusion characters will remain in the final data, and any data wolffd@0: % contained within or containing exclusion characters will not be wolffd@0: % converted to numerics. wolffd@0: % wolffd@0: % (c) Amos Storkey 2002 wolffd@0: % v b160702 wolffd@0: wolffd@0: % MATLAB is incapable of loading variable length lines or variable type values wolffd@0: % with a whole file command under the standard library sets. This mfile wolffd@0: % fills that gap. wolffd@0: if (nargin<4) wolffd@0: options=' '; wolffd@0: end; wolffd@0: dflag = []; wolffd@0: %Open file wolffd@0: fid=fopen(fname,'rt'); wolffd@0: %Cannot open: return -1 wolffd@0: if (fid<0) wolffd@0: lc=-1; wolffd@0: else wolffd@0: fullfile=fread(fid,'uchar=>char')'; wolffd@0: %Strip LF if free is set wolffd@0: if ~isempty(findstr(options,'free')) wolffd@0: fullfile=strrep(fullfile,char(10),''); wolffd@0: end; wolffd@0: %Find all delimiters wolffd@0: delimpos=[]; wolffd@0: for s=1:length(delim) wolffd@0: delimpos=[delimpos find(fullfile==delim(s))]; wolffd@0: end wolffd@0: %Find all eol wolffd@0: endpos=find(fullfile==char(10)); wolffd@0: endpos=setdiff(endpos,delimpos); wolffd@0: %find all exclusions wolffd@0: xclpos=[]; wolffd@0: for s=1:length(exclusions); wolffd@0: xclpos=[xclpos find(fullfile==exclusions(s))]; wolffd@0: end wolffd@0: sort(xclpos); wolffd@0: xclpos=[xclpos(1:2:end-1);xclpos(2:2:end)]; wolffd@0: %Combine eol and delimiters wolffd@0: jointpos=union(delimpos,endpos); wolffd@0: t=1; wolffd@0: %Remove delim/eol within exclusion pairs wolffd@0: removedelim=[]; wolffd@0: for s=1:length(jointpos) wolffd@0: if any((jointpos(s)>xclpos(1,:)) & (jointpos(s)