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