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.2 2008/08/25 12:43:10 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
0066
0067
0068
0069 for j=1:max(columns)
0070 if ismember(j, columns)
0071 scan_format = [scan_format '%n '];
0072 read_col = read_col + 1;
0073 replace = min(sort_col);
0074
0075 columns (columns==replace) = read_col;
0076 sort_col(sort_col==replace) = [];
0077 else
0078 scan_format = [scan_format '%*n '];
0079 end
0080 end
0081 if maxcol == 1
0082 scan_format = [deblank(scan_format) '%*[^\n]'];
0083 end
0084
0085
0086 if isempty(maxLines) || maxLines < 0
0087 maxLines = 1e20;
0088 end
0089 if maxLines == 1e20
0090 maxLines = numlines(fid);
0091 utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
0092 end
0093 fseek(fid, 0, 'bof');
0094
0095
0096 readlines = min(50000, maxLines);
0097 nlines = 0;
0098
0099
0100 f_data = zeros(maxLines, read_col);
0101
0102
0103 while ~feof(fid)
0104 f = deblank(fgetl(fid));
0105 if f(1) ~= comment_char
0106 break;
0107 end
0108 end
0109
0110 C = textscan(f, scan_format, 1, ...
0111 'CommentStyle', comment_char, ...
0112 'CollectOutput', 1);
0113 if any(isnan(C{:}))
0114 error('### Error in file format. Perhaps you specified more columns than the file contains?');
0115 end
0116
0117 fseek(fid, 0, 'bof');
0118
0119 while ~feof(fid) && nlines < maxLines
0120 C = textscan(fid, scan_format, readlines, ...
0121 'CommentStyle', comment_char, ...
0122 'CollectOutput', 1);
0123 f_data(nlines+1:nlines+length(C{1}),:) = C{1};
0124 nlines = nlines + length(C{1});
0125
0126 if isempty(C{1})
0127 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);
0128 end
0129
0130 utils.helper.msg(utils.const.msg.PROC2, ' - read %09d lines of %09d', nlines, maxLines', maxLines);
0131 end
0132 fclose(fid);
0133
0134
0135 if size(f_data,1) > nlines
0136 f_data = f_data(1:nlines, :);
0137 end
0138
0139
0140 if isempty(use_fs)
0141
0142
0143 if mod(length(columns),2) ~= 0
0144 error('### the numbers in columns must be straight');
0145 end
0146
0147 for lauf = 1:length(columns)/2
0148
0149 data_x_axes = f_data(:, columns(lauf*2-1));
0150 data_y_axes = f_data(:, columns(lauf*2));
0151
0152
0153 ao_data = [];
0154 switch data_type
0155 case 'tsdata'
0156 ao_data = tsdata( data_x_axes, data_y_axes);
0157 case 'fsdata'
0158 ao_data = fsdata( data_x_axes, data_y_axes);
0159 case 'cdata'
0160 error ('### please code me up')
0161 case 'xydata'
0162 ao_data = xydata( data_x_axes, data_y_axes);
0163 otherwise
0164 error('### unknown data type ''%s''', data_type);
0165 end
0166 aa = ao(ao_data);
0167 aa.setName(sprintf('%s_%02d_%02d', file_name, orig_col(lauf*2-1), orig_col(lauf*2)), 'internal');
0168
0169 pli = pli.pset('columns', [orig_col(lauf*2-1) orig_col(lauf*2)]);
0170 aa.addHistory(mi, pli, [], []);
0171
0172
0173 description_pl = find(pl, 'description');
0174 if ~isempty(description_pl)
0175 aa.description = description_pl;
0176 end
0177
0178 a = [a aa];
0179
0180 end
0181
0182
0183 else
0184
0185 for lauf = 1:length(columns)
0186
0187 data_y_axes = f_data(:, columns(lauf));
0188
0189
0190 ao_data = [];
0191 switch data_type
0192 case 'tsdata'
0193 ao_data = tsdata(data_y_axes, use_fs);
0194 case 'fsdata'
0195 ao_data = fsdata(data_y_axes, use_fs);
0196 case 'cdata'
0197 error ('### please code me up')
0198 case 'xydata'
0199 error ('### please code me up')
0200 otherwise
0201 error('### unknown data type ''%s''', data_type);
0202 end
0203 aa = ao(ao_data);
0204 aa.setName(sprintf('%s_%02d', file_name, orig_col(lauf)), 'internal');
0205 pli = pli.pset('columns', orig_col(lauf));
0206 aa.addHistory(mi, pli, [], []);
0207
0208
0209 description_pl = find(pl, 'description');
0210 if ~isempty(description_pl)
0211 aa.description = description_pl;
0212 end
0213
0214 a = [a aa];
0215
0216 end
0217
0218 end
0219
0220
0221 for ii = 1:length(a)
0222
0223 for jj = 1:pli.nparams
0224
0225 cmd = ['set' pli.params(jj).key(1) lower(pli.params(jj).key(2:end))];
0226
0227 if ismethod(a(ii), cmd)
0228 if iscell(pli.params(jj).val) && length(pli.params(jj).val) == length(a)
0229
0230 feval(cmd, a(ii), pli.params(jj).val{ii}, 'internal');
0231
0232 a(ii).hist.plistUsed.pset(pli.params(jj).key, pli.params(jj).val{ii});
0233 else
0234
0235 feval(cmd, a(ii), pli.params(jj).val, 'internal');
0236 end
0237 end
0238
0239 end
0240
0241 end
0242
0243 end
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 function lines = numlines(fid)
0263
0264 lines = 0;
0265 nlchr = uint8(sprintf('\n'));
0266 bsize = 4 * 256 * 8192;
0267
0268 while ~feof(fid)
0269 block = fread(fid, bsize, '*uint8');
0270 lines = lines + sum(block == nlchr);
0271 end
0272 if ~isempty(block)
0273 lines = lines + double(block(end) ~= nlchr);
0274 end
0275 end
0276