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