0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % FROMDATAFILE Construct an ao from filename AND parameter list
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % FUNCTION: fromDatafile
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % DESCRIPTION: Construct an ao from filename AND parameter list
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % CALL: a = fromFilenameAndPlist(a, pli)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 % PARAMETER: a: empty ao-object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % pli: plist-object (must contain the filename)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % VERSION: $Id: fromDatafile.m,v 1.51 2011/10/12 20:30:51 mauro Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 function a = fromDatafile(ain, pli)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 utils.helper.msg(utils.const.msg.PROC1, 'constructing from filename and/or plist');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 VERSION = '$Id: fromDatafile.m,v 1.51 2011/10/12 20:30:51 mauro Exp $';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % get AO info
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 mi = ao.getInfo('ao', 'From ASCII File');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % Set the method version string in the minfo object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 mi.setMversion([VERSION '-->' mi.mversion]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % Get filename
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 file_name = find(pli, 'filename');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 file_path = find(pli, 'filepath');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 [filePath, fileName, ext] = fileparts(file_name);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % Add the file extenstion to the fileName
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 fileName = strcat(fileName, ext);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % Define the path of the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 if ~isempty(file_path) && ~isempty(filePath)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % Do nothing because we will use filePath
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 elseif ~isempty(file_path)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 filePath = file_path;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 elseif ~isempty(filePath)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 % Do nothing because we will use filePath
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 filePath = pwd();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 absolutePathname = fullfile(filePath, fileName);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % Check if the abolute Pathname exist
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 if ~exist(absolutePathname, 'file')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 absolutePathname = fileName;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 %%%%%%%%%% Get default parameter list %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 dpl = ao.getDefaultPlist('From ASCII File');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 pl = applyDefaults(dpl, pli);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 pl = pset(pl, 'filename', fileName);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 pl = pset(pl, 'filepath', filePath);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 data_type = find(pl, 'type');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 columns = find(pl, 'columns');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 maxLines = find(pl, 'maxlines');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 comment_char = find(pl, 'comment_char');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 delimiter = find(pl, 'delimiter');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 use_fs = find(pl, 'fs');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 a = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 orig_col = columns; % necessary for the history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 t0 = find(pl, 't0');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 %%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 if strcmpi(data_type, 'cdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 use_fs = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 %%%%%%%%%% read file %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 [fid,msg] = fopen (absolutePathname, 'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 if (fid < 0)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 error ('### can not open file: %s \n### error msg: %s', absolutePathname, msg);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 %%%%%%%%%% create scan format: '%f %f %f %f %f %*[^\n]' %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 scan_format = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 read_col = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 %%%%%%%%%% Read first comment and empty lines %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 while ~feof(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 fidPos = ftell(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 fline = fgetl(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 % check if there was an error reading the file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 [errmsg, errnum] = ferror(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 if errnum ~= 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 error('### an error happened reading the first part of the file: %s', errmsg)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 if fline == -1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 % we hit the end of file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 error('### the file is empty!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 fline = strtrim(fline);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 if ~isempty(fline) && ~(~isempty(comment_char) && strncmp(fline, comment_char, numel(comment_char)))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 fseek(fid, fidPos, 'bof');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 break;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 %%%%%%%%%% Get/Count max number of lines %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 if isempty(maxLines)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 maxLines = numLines(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 %%%%%%%%%% Check max number of columns %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 maxColumns = numCols(fid, delimiter);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 if isempty(columns) && strcmpi(data_type, 'cdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 columns = 1:maxColumns;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 orig_col = columns;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 if maxColumns < max(columns)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 error('### The file doesn''t have more than [%d] columns. But you want to read the column [%d].', maxColumns, max(columns));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 %%%%%%%%%% Check number of columns %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 if isempty(columns) && ~strcmpi(data_type, 'cdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 error('### Please specify at least one column number to read the data file.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 %%% preallocate data array
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 f_data = zeros(maxLines, numel(unique(columns)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 %%% check if using robust read: 'yes'/'no' or true/false or 'true'/'false'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 robust = utils.prog.yes2true(find(pl, 'Robust'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 if robust
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 f_data = robustRead(fid, f_data, columns, orig_col);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 %%% Based on skipping the not used columns we have to transform the columns.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 %%% We must transform the columns [ 2 5 2 6 5 7] to [ 1 2 1 3 2 4]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 %%% In each loop we have to replace the corresponding value. In the first loop
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 %%% the first minimum, in the second loop the second minimum, ... with the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 %%% current loop number.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 sort_col = sort(columns);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 for jj = 1:max(columns)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 if ismember(jj, columns)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 scan_format = [scan_format '%n'];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 read_col = read_col + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 replace = min(sort_col);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 columns (columns == replace) = read_col;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 sort_col(sort_col == replace) = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 scan_format = [scan_format '%*n'];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 scan_format = [deblank(scan_format) '%*[^\n]'];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 %%%%%%%%%% Read data %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 readlines = min(50000, maxLines);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 nlines = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 %%% read file to end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 while ~feof(fid) && nlines < maxLines
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 if isempty(comment_char) && isempty(delimiter)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 C = textscan(fid, scan_format, readlines);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 elseif isempty(comment_char) && ~isempty(delimiter)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 C = textscan(fid, scan_format, readlines, 'Delimiter', delimiter);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 elseif ~isempty(comment_char) && isempty(delimiter)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 C = textscan(fid, scan_format, readlines, 'CommentStyle', comment_char);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 C = textscan(fid, scan_format, readlines, 'CommentStyle', comment_char, 'Delimiter', delimiter);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 if isempty(C{1}) && nlines == 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 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);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 f_data(nlines+1:nlines+size(C{1},1),:) = cell2mat(C);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 nlines = nlines + length(C{1});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186 utils.helper.msg(utils.const.msg.PROC2, 'read %09d lines of %09d', nlines, maxLines);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 %%% get only the data we want
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 if size(f_data,1) > nlines
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 f_data = f_data(1:nlines, :);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 catch ME
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 % An error occurred during the scan
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 fclose(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 rethrow(ME);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 % The scan was successful
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 fclose(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 %%%%%%%%%% Create for each column pair the data object %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 % This is a list of keys which support multiple values. We need to
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 % process the history plist to account for this so that the hisotry of a
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 % single object only contains the value that the user intended that
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 % object to get.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 % TODO: find a nicer way to specify this?
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 keys = {'name', 'description', 'xunits', 'yunits', 't0'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 if isempty(use_fs)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 %%%%%%%%%% The numbers in columns must be even %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 if length(columns) == 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 if ~strcmp(data_type, 'cdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 error('A single column file with no sample rate set can only be used to build a cdata AO');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 if mod(length(columns),2) ~= 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 error('### The numbers in columns must be even or you forgot to specify the ''fs'' parameter');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 N = length(columns)/2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 for lauf = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 data_x_axes = f_data(:, columns(lauf*2-1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 data_y_axes = f_data(:, columns(lauf*2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 % create data object corresponding to the parameter list
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 ao_data = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 switch lower(data_type)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 case 'tsdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 ao_data = tsdata(data_x_axes, data_y_axes);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 case 'fsdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 ao_data = fsdata(data_x_axes, data_y_axes);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 case 'cdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 error('### This should not happen!');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 case 'xydata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 ao_data = xydata(data_x_axes, data_y_axes);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 otherwise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 error('### unknown data type ''%s''', data_type);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247 aa = ao(ao_data);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 % set up the history plist for this object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 plhist = pl.processForHistory(N, lauf, keys);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 plhist.pset('columns', [orig_col(lauf*2-1) orig_col(lauf*2)]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 % Add history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 aa.addHistory(mi, plhist, [], []);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 a = [a aa];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 %%%%%%%%%% Create for each column AND fs a data object %%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 else % isempty(use_fs)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 N = length(columns);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 for lauf = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 data_y_axes = f_data(:, columns(lauf));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 % create data object corresponding to the parameter list
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 ao_data = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 switch lower(data_type)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 case 'tsdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 ao_data = tsdata(data_y_axes, use_fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274 case 'fsdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 ao_data = fsdata(data_y_axes, use_fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 case 'cdata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 % Special case for cdata-objects.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 % Is the user specify some columns then he will get for each
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 % column an AO. If don't specify a columns then he will all data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 % in a single AO.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 if isempty(pl.find('columns'))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 % Create only one AO with all data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 a = ao(f_data);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 a.name = fileName;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 a.addHistory(mi, pl, [], []);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 break
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 % Create for each column a single AO
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289 ao_data = cdata(data_y_axes);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 case 'xydata'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292 ao_data = xydata(data_y_axes);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 otherwise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 error('### unknown data type ''%s''', data_type);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 aa = ao(ao_data);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298 % set up the history plist for this object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 plhist = pl.processForHistory(N, lauf, keys);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 plhist.pset('columns', orig_col(lauf));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 % Add history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 aa.addHistory(mi, plhist, [], []);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 a = [a aa];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312 % for tsdata and fsdata we support setting the t0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 if any(strcmpi(data_type, {'tsdata', 'fsdata'}))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314 a.setT0(t0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 % set xunits if we don't have a cdata
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318 if ~strcmpi(data_type, 'cdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 xunits = pl.find('xunits');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 if isempty(xunits)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 if strcmpi(data_type, 'tsdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322 xunits = 's';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 elseif strcmpi(data_type, 'fsdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324 xunits = 'Hz';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 % do nothing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329 a.setXunits(xunits);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 % set yunits
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333 a.setYunits(pl.find('yunits'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 % Set object properties
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 a.setObjectProperties(pl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 % Local Functions %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346 % FUNCTION: numLines
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 % SYNTAX: count = numLines(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 % DESCRIPTION: Returns the number of lines in an ASCII file. This method
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 % doesn't change the position of the file identifier (fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353 % HISTORY: 02-08-2002 Peter Acklam, CSSM post
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 % Creation.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 function lines = numLines(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 fidPos = ftell(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 block = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 lines = 0; % number of lines in file
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363 nlchr = uint8(sprintf('\n')); % newline chr as uint8
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 bsize = 4 * 256 * 8192; % block size to read
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 while ~feof(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 block = fread(fid, bsize, '*uint8');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 lines = lines + sum(block == nlchr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 if ~isempty(block) % in case file is empty
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 lines = lines + double(block(end) ~= nlchr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373 fseek(fid, fidPos, 'bof');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 % FUNCTION: numCols
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 % SYNTAX: count = numCols(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 % DESCRIPTION: Returns the number of columns in an ASCII file. This method
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 % doesn't change the position of the file identifier (fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387 function ncols = numCols(fid, delimiter)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 fidPos = ftell(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 line = fgetl(fid);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 if ischar(line)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392 if nargin == 1 || isempty(delimiter)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 C = textscan(line, '%s');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 C = textscan(line, '%s', 'Delimiter', delimiter);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 C = C{1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398 ncols = numel(C);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 ncols = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 fseek(fid, fidPos, 'bof');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 % A robust and slow data reader
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407 function f_data = robustRead(fid, f_data, columns, orig_cols)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 cols = unique(columns);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 ocols = unique(orig_cols);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 Nline = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 while ~feof(fid)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 % read and parse line
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 tokens = sscanf(fgets(fid), '%f');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 % parse tokens
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 if ~isempty(tokens)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 f_data(Nline, cols) = tokens(ocols);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 if mod(Nline, 1000) == 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419 utils.helper.msg(utils.const.msg.PROC2, 'lines read: %d', Nline);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 Nline = Nline + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425 % drop empty lines
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 f_data = f_data(1:Nline-1, :);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428 end
|