0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 function a = fromDatafile(ain, pli)
0021
0022 utils.helper.msg(utils.const.msg.PROC1, 'constructing from filename and/or plist');
0023
0024 VERSION = '$Id: fromDatafile.m,v 1.5 2008/09/07 18:08:03 hewitson Exp $';
0025
0026
0027 mi = ao.getInfo('ao', 'From ASCII File');
0028
0029
0030 mi.setMversion([VERSION '-->' mi.mversion]);
0031
0032
0033 file_name = find(pli, 'filename');
0034
0035 [pathstr, f_name, ext] = fileparts(file_name);
0036
0037
0038 dpl = ao.getDefaultPlist('From ASCII File');
0039 pl = combine(pli, dpl);
0040
0041 pl = pset(pl, 'filename', [f_name ext]);
0042 pl = pset(pl, 'filepath', pathstr);
0043
0044 data_type = find (pl, 'type');
0045 columns = find (pl, 'columns');
0046 maxLines = find (pl, 'maxlines');
0047 comment_char = find (pl, 'comment_char');
0048 use_fs = find (pl, 'use_fs');
0049 a = [];
0050 orig_col = columns;
0051
0052
0053 [fid,msg] = fopen (file_name, 'r');
0054 if (fid < 0)
0055 error ('### can not open file: %s \n\n### error msg:',file_name, msg);
0056 end
0057
0058
0059 maxcol = max(columns);
0060 scan_format = '';
0061 read_col = 0;
0062 sort_col = sort(columns);
0063
0064
0065 if isempty(maxLines) || maxLines < 0
0066 maxLines = 1e20;
0067 end
0068 if maxLines == 1e20
0069 maxLines = numlines(fid);
0070 utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
0071 end
0072 fseek(fid, 0, 'bof');
0073
0074
0075 readlines = min(50000, maxLines);
0076 nlines = 0;
0077
0078
0079
0080
0081
0082
0083 for j=1:max(columns)
0084 if ismember(j, columns)
0085 scan_format = [scan_format '%n '];
0086 read_col = read_col + 1;
0087 replace = min(sort_col);
0088
0089 columns (columns==replace) = read_col;
0090 sort_col(sort_col==replace) = [];
0091 else
0092 scan_format = [scan_format '%*n '];
0093 end
0094 end
0095 scan_format = [deblank(scan_format) '%*[^\n]'];
0096
0097
0098 f_data = zeros(maxLines, read_col);
0099
0100 if strcmpi(find(pl, 'Robust'), 'yes')
0101
0102 f_data = robustRead(fid, f_data, columns, orig_col);
0103
0104 else
0105
0106
0107
0108 while ~feof(fid)
0109 f = deblank(fgetl(fid));
0110 if ~isempty(f)
0111 if f(1) ~= comment_char
0112 break;
0113 end
0114 end
0115 end
0116
0117 C = textscan(f, scan_format, 1, ...
0118 'CommentStyle', comment_char, ...
0119 'CollectOutput', 1);
0120 if any(isnan(C{:}))
0121 error('### Error in file format. Perhaps you specified more columns than the file contains?');
0122 end
0123
0124 fseek(fid, 0, 'bof');
0125
0126 while ~feof(fid) && nlines < maxLines
0127 C = textscan(fid, scan_format, readlines, ...
0128 'CommentStyle', comment_char, ...
0129 'CollectOutput', 1);
0130 f_data(nlines+1:nlines+size(C{1},1),:) = C{1};
0131 nlines = nlines + length(C{1});
0132
0133 if isempty(C{1})
0134 error('\n### There are no data.\n### Did you use the right comment character?\n### The current comment character is: [%s]\n### Use a parameter list with the parameter:\n### plist(''comment_char'', ''%%'')', comment_char);
0135 end
0136
0137 utils.helper.msg(utils.const.msg.PROC2, 'read %09d lines of %09d', nlines, maxLines);
0138 end
0139 fclose(fid);
0140
0141
0142 if size(f_data,1) > nlines
0143 f_data = f_data(1:nlines, :);
0144 end
0145 end
0146
0147
0148
0149 if isempty(use_fs)
0150
0151
0152 if mod(length(columns),2) ~= 0
0153 error('### the numbers in columns must be straight');
0154 end
0155
0156 for lauf = 1:length(columns)/2
0157
0158 data_x_axes = f_data(:, columns(lauf*2-1));
0159 data_y_axes = f_data(:, columns(lauf*2));
0160
0161
0162 ao_data = [];
0163 switch data_type
0164 case 'tsdata'
0165 ao_data = tsdata( data_x_axes, data_y_axes);
0166 case 'fsdata'
0167 ao_data = fsdata( data_x_axes, data_y_axes);
0168 case 'cdata'
0169 error ('### please code me up')
0170 case 'xydata'
0171 ao_data = xydata( data_x_axes, data_y_axes);
0172 otherwise
0173 error('### unknown data type ''%s''', data_type);
0174 end
0175 aa = ao(ao_data);
0176 aa.setName(sprintf('%s_%02d_%02d', file_name, orig_col(lauf*2-1), orig_col(lauf*2)), 'internal');
0177
0178 pl = pl.pset('columns', [orig_col(lauf*2-1) orig_col(lauf*2)]);
0179 aa.addHistory(mi, pl, [], []);
0180
0181
0182 description_pl = find(pl, 'description');
0183 if ~isempty(description_pl)
0184 aa.description = description_pl;
0185 end
0186
0187 a = [a aa];
0188
0189 end
0190
0191
0192 else
0193
0194 for lauf = 1:length(columns)
0195
0196 data_y_axes = f_data(:, columns(lauf));
0197
0198
0199 ao_data = [];
0200 switch data_type
0201 case 'tsdata'
0202 ao_data = tsdata(data_y_axes, use_fs);
0203 case 'fsdata'
0204 ao_data = fsdata(data_y_axes, use_fs);
0205 case 'cdata'
0206 error ('### please code me up')
0207 case 'xydata'
0208 error ('### please code me up')
0209 otherwise
0210 error('### unknown data type ''%s''', data_type);
0211 end
0212 aa = ao(ao_data);
0213 aa.setName(sprintf('%s_%02d', file_name, orig_col(lauf)), 'internal');
0214 pl = pl.pset('columns', orig_col(lauf));
0215 aa.addHistory(mi, pl, [], []);
0216
0217
0218 description_pl = find(pl, 'description');
0219 if ~isempty(description_pl)
0220 aa.description = description_pl;
0221 end
0222
0223 a = [a aa];
0224
0225 end
0226
0227 end
0228
0229
0230 for ii = 1:length(a)
0231
0232 for jj = 1:pli.nparams
0233
0234 cmd = ['set' pli.params(jj).key(1) lower(pli.params(jj).key(2:end))];
0235
0236 if ismethod(a(ii), cmd)
0237 if iscell(pli.params(jj).val) && length(pli.params(jj).val) == length(a)
0238
0239 feval(cmd, a(ii), pli.params(jj).val{ii}, 'internal');
0240
0241 a(ii).hist.plistUsed.pset(pli.params(jj).key, pli.params(jj).val{ii});
0242 else
0243
0244 feval(cmd, a(ii), pli.params(jj).val, 'internal');
0245 end
0246 end
0247
0248 end
0249
0250 end
0251
0252 end
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 function lines = numlines(fid)
0272
0273 lines = 0;
0274 nlchr = uint8(sprintf('\n'));
0275 bsize = 4 * 256 * 8192;
0276
0277 while ~feof(fid)
0278 block = fread(fid, bsize, '*uint8');
0279 lines = lines + sum(block == nlchr);
0280 end
0281 if ~isempty(block)
0282 lines = lines + double(block(end) ~= nlchr);
0283 end
0284 end
0285
0286
0287 function f_data = robustRead(fid, f_data, columns, orig_cols)
0288
0289 cols = unique(columns);
0290 ocols = unique(orig_cols);
0291 Nline = 1;
0292 while ~feof(fid)
0293
0294 tokens = sscanf(fgets(fid), '%f');
0295
0296 if ~isempty(tokens)
0297 f_data(Nline, cols) = tokens(ocols);
0298 if mod(Nline, 1000) == 0
0299 utils.helper.msg(utils.const.msg.PROC2, 'lines read: %d', Nline);
0300 end
0301 Nline = Nline + 1;
0302 end
0303 end
0304
0305
0306 f_data = f_data(1:Nline-1, :);
0307
0308 end