annotate _FullBNT/KPMtools/loadcell.m @ 9:4ea6619cb3f5 tip

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