annotate toolboxes/FullBNT-1.0.7/KPMtools/loadcell.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [lc,dflag,dattype]=loadcell(fname,delim,exclusions,options);
wolffd@0 2 %function [lc,dflag,numdata]=loadcell(fname,delim,exclusions);
wolffd@0 3 %
wolffd@0 4 % loadcell loads a cell array with character delimited
wolffd@0 5 % data, which can have variable length lines and content.
wolffd@0 6 % Numeric values are converted from string to double
wolffd@0 7 % unless options is a string containing 'string'.
wolffd@0 8 %
wolffd@0 9 % loadcell is for use with small datasets. It is not optimised
wolffd@0 10 % for large datasets.
wolffd@0 11 %
wolffd@0 12 % fname is the filename to be loaded
wolffd@0 13 %
wolffd@0 14 % delim is/are the relevant delimiter(s). If char(10) is included
wolffd@0 15 % newlines are simply treated as delimiters and a 1-d array is created.
wolffd@0 16 %
wolffd@0 17 % exclusions are the set of characters to be treated as paired
wolffd@0 18 % braces: line ends or delimiters within braces are ignored.
wolffd@0 19 % braces are single characters and any brace can pair with
wolffd@0 20 % any other brace: no type pair checking is done.
wolffd@0 21 %
wolffd@0 22 % options can be omitted or can contain 'string' if no numeric
wolffd@0 23 % conversion is required, 'single' if multiple adjacent seperators
wolffd@0 24 % should not be treated as one, 'free' if all linefeeds should be stripped
wolffd@0 25 % first and 'empty2num' if empty fields are to be treated as numeric
wolffd@0 26 % zeros rather than an empty character set. Combine options using
wolffd@0 27 % concatenation.
wolffd@0 28 %
wolffd@0 29 % lc is a cell array containing the loaded data.
wolffd@0 30 %
wolffd@0 31 % dflag is a set of flags denoting the (i,j) values where data was entered
wolffd@0 32 % dflag(i,j)=1 implies lc(i,j) was loaded from the data, and not just set
wolffd@0 33 % to empty, say, by default.
wolffd@0 34 %
wolffd@0 35 % numdata is an array numdata(i,j)=NaN implies
wolffd@0 36 % lc(i,j) is a string, otherwise it stores the number at i,j.
wolffd@0 37 % This will occur regardless of whether the 'string' option is set.
wolffd@0 38 %
wolffd@0 39 % lc will return -1 if the file is not found or could not be
wolffd@0 40 % opened.
wolffd@0 41 %
wolffd@0 42 % Hint: numdata+(1/dflag-1) provides a concise descriptor for the numeric data
wolffd@0 43 % Inf=not loaded
wolffd@0 44 % NaN=was string or empty set.
wolffd@0 45 % otherwise numeric
wolffd@0 46 %
wolffd@0 47 % EXAMPLE
wolffd@0 48 %
wolffd@0 49 %[a,b]=loadcell('resultsfile',[',' char(9)],'"','single-string');
wolffd@0 50 % will load file 'resultsfile' into variable a, treating any of tab or
wolffd@0 51 % comma as delimiters. Delimiters or carriage returns lying
wolffd@0 52 % between two double inverted commas will be ignored. Two adjacent delimiters
wolffd@0 53 % will count twice, and all data will be kept as a string.
wolffd@0 54 %
wolffd@0 55 % Note: in space-separated data 'single' would generally be omitted,
wolffd@0 56 % wheras in comma-seperated data it would be included.
wolffd@0 57 %
wolffd@0 58 % Note the exclusion characters will remain in the final data, and any data
wolffd@0 59 % contained within or containing exclusion characters will not be
wolffd@0 60 % converted to numerics.
wolffd@0 61 %
wolffd@0 62 % (c) Amos Storkey 2002
wolffd@0 63 % v b160702
wolffd@0 64
wolffd@0 65 % MATLAB is incapable of loading variable length lines or variable type values
wolffd@0 66 % with a whole file command under the standard library sets. This mfile
wolffd@0 67 % fills that gap.
wolffd@0 68 if (nargin<4)
wolffd@0 69 options=' ';
wolffd@0 70 end;
wolffd@0 71 dflag = [];
wolffd@0 72 %Open file
wolffd@0 73 fid=fopen(fname,'rt');
wolffd@0 74 %Cannot open: return -1
wolffd@0 75 if (fid<0)
wolffd@0 76 lc=-1;
wolffd@0 77 else
wolffd@0 78 fullfile=fread(fid,'uchar=>char')';
wolffd@0 79 %Strip LF if free is set
wolffd@0 80 if ~isempty(findstr(options,'free'))
wolffd@0 81 fullfile=strrep(fullfile,char(10),'');
wolffd@0 82 end;
wolffd@0 83 %Find all delimiters
wolffd@0 84 delimpos=[];
wolffd@0 85 for s=1:length(delim)
wolffd@0 86 delimpos=[delimpos find(fullfile==delim(s))];
wolffd@0 87 end
wolffd@0 88 %Find all eol
wolffd@0 89 endpos=find(fullfile==char(10));
wolffd@0 90 endpos=setdiff(endpos,delimpos);
wolffd@0 91 %find all exclusions
wolffd@0 92 xclpos=[];
wolffd@0 93 for s=1:length(exclusions);
wolffd@0 94 xclpos=[xclpos find(fullfile==exclusions(s))];
wolffd@0 95 end
wolffd@0 96 sort(xclpos);
wolffd@0 97 xclpos=[xclpos(1:2:end-1);xclpos(2:2:end)];
wolffd@0 98 %Combine eol and delimiters
wolffd@0 99 jointpos=union(delimpos,endpos);
wolffd@0 100 t=1;
wolffd@0 101 %Remove delim/eol within exclusion pairs
wolffd@0 102 removedelim=[];
wolffd@0 103 for s=1:length(jointpos)
wolffd@0 104 if any((jointpos(s)>xclpos(1,:)) & (jointpos(s)<xclpos(2,:)))
wolffd@0 105 removedelim(t)=jointpos(s);
wolffd@0 106 t=t+1;
wolffd@0 107 end;
wolffd@0 108
wolffd@0 109 end
wolffd@0 110 %and add start point
wolffd@0 111 jointpos=[0 setdiff(jointpos,removedelim)];
wolffd@0 112 i=1;
wolffd@0 113 j=1;
wolffd@0 114 posind=1;
wolffd@0 115 multflag=isempty(findstr(options,'single'));
wolffd@0 116 stringflag=~isempty(findstr(options,'string'));
wolffd@0 117 emptnum=~isempty(findstr(options,'empty2num'));
wolffd@0 118 %Run through
wolffd@0 119 while (posind<(length(jointpos)))
wolffd@0 120 %Get current field
wolffd@0 121 tempstr=fullfile(jointpos(posind)+1:jointpos(posind+1)-1);
wolffd@0 122 %If empty only continue if adjacent delim count.
wolffd@0 123 if ~(isempty(tempstr) & multflag);
wolffd@0 124 %This ij is set
wolffd@0 125 dflag(i,j)=1;
wolffd@0 126 %Convert to num
wolffd@0 127 tempno=str2double([tempstr]);
wolffd@0 128 %If emptystring convert to zero if emptnum set
wolffd@0 129 if (isempty(tempstr) & emptnum)
wolffd@0 130 tempno=0;
wolffd@0 131 end;
wolffd@0 132 %Set dattype to no (or NaN if not a num
wolffd@0 133 dattype(i,j)=tempno;
wolffd@0 134 %If NaN set lc to string else to num if stringflag not set
wolffd@0 135 if (isnan(tempno) | stringflag)
wolffd@0 136 lc{i,j}=tempstr;
wolffd@0 137 else
wolffd@0 138 lc{i,j}=tempno;
wolffd@0 139 end;
wolffd@0 140 %Next j
wolffd@0 141 j=j+1;
wolffd@0 142 end;
wolffd@0 143 %If eol inc i and reset j
wolffd@0 144 if ismember(jointpos(posind+1),endpos)
wolffd@0 145 i=i+1;
wolffd@0 146 j=1;
wolffd@0 147 end;
wolffd@0 148 %Inc to next delim
wolffd@0 149 posind=posind+1;
wolffd@0 150 end;
wolffd@0 151 end;
wolffd@0 152 %Logicalise dflag
wolffd@0 153 dflag=logical(dflag);