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