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

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