root/applications/doprava/matlab/itload.m @ 1047

Revision 843, 10.7 kB (checked in by smidl, 15 years ago)

modification of itload to read names staring with numbers by appending "x" in front

  • Property svn:eol-style set to native
Line 
1% File:   itload.m
2% Brief:  Load an IT++ itfile content to Matlab/Octave workspace
3% Author: Tony Ottosson and Adam Piatyszek
4%
5% Usage: itload("fname.it")
6%
7% This functions loads all variables from an IT++ file format to the
8% Matlab/Octave workspace.
9%
10% -------------------------------------------------------------------------
11%
12% IT++ - C++ library of mathematical, signal processing, speech processing,
13%        and communications classes and functions
14%
15% Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
16%
17% This program is free software; you can redistribute it and/or modify
18% it under the terms of the GNU General Public License as published by
19% the Free Software Foundation; either version 2 of the License, or
20% (at your option) any later version.
21%
22% This program is distributed in the hope that it will be useful,
23% but WITHOUT ANY WARRANTY; without even the implied warranty of
24% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25% GNU General Public License for more details.
26%
27% You should have received a copy of the GNU General Public License
28% along with this program; if not, write to the Free Software
29% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30%
31% -------------------------------------------------------------------------
32
33function itload(fname)
34
35[fid, err_msg] = fopen(fname, 'rb', 'ieee-le');
36if (fid == -1)
37  fname = [fname '.it'];
38  [fid, err_msg2] = fopen(fname, 'rb', 'ieee-le');
39  if (fid == -1)
40    error(err_msg);
41  end
42end
43
44% Check file size
45fseek(fid, 0, 'eof');
46file_size = ftell(fid);
47fseek(fid, 0, 'bof');
48
49% Read "IT++" magic string
50[d, n] = fread(fid, 5, 'char');
51if (n ~= 5 | d(1:4) ~= [73 84 43 43]')
52  error('Not an IT++ file!');
53end
54
55% Check the IT++ file version
56if (d(5) ~= 3)
57  error('Only IT++ file version 3 is supported by this function!');
58end
59
60while (1)
61  p = ftell(fid); % Save current file position
62  [d1, n] = fread(fid, 3, 'uint64'); % Read header, data, and total block sizes
63  name = fgetstr(fid); % Read current variable name
64  type = fgetstr(fid); % Read current variable type
65  fseek(fid, p+d1(1), 'bof'); % Skip header bytes
66
67  first_char = int16(name(1));
68  if (first_char>=48 & first_char<=57) % name starts with char
69      name = ['x' name];
70  end
71 
72  if (length(type) == 0) % A deleted entry -> skip it
73
74  % --- bin ---
75  elseif (strcmp(type, 'bin'))
76    [d, n] = fread(fid, 1, 'char');
77    assignin('caller', name, d);
78  % --- int8 (char) ---
79  elseif (strcmp(type, 'int8'))
80    [d, n] = fread(fid, 1, 'int8');
81    assignin('caller', name, d);
82  % --- int16 (short) ---
83  elseif (strcmp(type, 'int16'))
84    [d, n] = fread(fid, 1, 'int16');
85    assignin('caller', name, d);
86  % --- int32 (int) ---
87  elseif (strcmp(type, 'int32'))
88    [d, n] = fread(fid, 1, 'int32');
89    assignin('caller', name, d);
90  % --- float32 (float) ---
91  elseif (strcmp(type, 'float32'))
92    [d, n] = fread(fid, 1, 'float32');
93    assignin('caller', name, d);
94  % --- float64 (double) ---
95  elseif (strcmp(type, 'float64'))
96    [d, n] = fread(fid, 1, 'float64');
97    assignin('caller', name, d);
98  % --- cfloat32 (complex<float>) ---
99  elseif (strcmp(type, 'cfloat32'))
100    [d, n] = fread(fid, 2, 'float32');
101    d = complex(d(1), d(2));
102    assignin('caller', name, d);
103  % --- cfloat64 (complex<double>) ---
104  elseif (strcmp(type, 'cfloat64'))
105    [d, n] = fread(fid, 2, 'float64');
106    d = complex(d(1), d(2));
107    assignin('caller', name, d);
108
109  % --- bvec ---
110  elseif (strcmp(type, 'bvec'))
111    [size, n] = fread(fid, 1, 'uint64');
112    [d, n] = fread(fid, size, 'char');
113    assignin('caller', name, d);
114  % --- svec ---
115  elseif (strcmp(type, 'svec'))
116    [size, n] = fread(fid, 1, 'uint64');
117    [d, n] = fread(fid, size, 'int16');
118    assignin('caller', name, d);
119  % --- ivec ---
120  elseif (strcmp(type, 'ivec'))
121    [size, n] = fread(fid, 1, 'uint64');
122    [d, n] = fread(fid, size, 'int32');
123    assignin('caller', name, d);
124  % --- fvec ---
125  elseif (strcmp(type, 'fvec'))
126    [size, n] = fread(fid, 1, 'uint64');
127    [d, n] = fread(fid, size, 'float32');
128    assignin('caller', name, d);
129  % --- dvec ---
130  elseif (strcmp(type, 'dvec'))
131    [size, n] = fread(fid, 1, 'uint64');
132    [d, n] = fread(fid, size, 'float64');
133    assignin('caller', name, d);
134  % --- fcvec ---
135  elseif (strcmp(type, 'fcvec'))
136    [size, n] = fread(fid, 1, 'uint64');
137    [d, n] = fread(fid, size*2, 'float32');
138    d = complex(d(1:2:end), d(2:2:end));
139    assignin('caller', name, d);
140  % --- dcvec ---
141  elseif (strcmp(type, 'dcvec'))
142    [size, n] = fread(fid, 1, 'uint64');
143    [d, n] = fread(fid, size*2, 'float64');
144    d = complex(d(1:2:end), d(2:2:end));
145    assignin('caller', name, d);
146  % --- string ---
147  elseif (strcmp(type, 'string'))
148    [size, n] = fread(fid, 1, 'uint64');
149    [d, n] = fread(fid, size, 'char');
150    d = char(d);
151    assignin('caller', name, d);
152
153  % --- bmat ---
154  elseif (strcmp(type, 'bmat'))
155    [size, n] = fread(fid, 2, 'uint64');
156    [d, n] = fread(fid, size', 'char');
157    assignin('caller', name, d);
158  % --- smat ---
159  elseif (strcmp(type, 'smat'))
160    [size, n] = fread(fid, 2, 'uint64');
161    [d, n] = fread(fid, size', 'int16');
162    assignin('caller', name, d);
163  % --- imat ---
164  elseif (strcmp(type, 'imat'))
165    [size, n] = fread(fid, 2, 'uint64');
166    [d, n] = fread(fid, size', 'int32');
167    assignin('caller', name, d);
168  % --- fmat ---
169  elseif (strcmp(type, 'fmat'))
170    [size, n] = fread(fid, 2, 'uint64');
171    [d, n] = fread(fid, size', 'float32');
172    assignin('caller', name, d);
173  % --- dmat ---
174  elseif (strcmp(type, 'dmat'))
175    [size, n] = fread(fid, 2, 'uint64');
176    [d, n] = fread(fid, size', 'float64');
177    assignin('caller', name, d);
178  % --- fcmat ---
179  elseif (strcmp(type, 'fcmat'))
180    [size, n] = fread(fid, 2, 'uint64');
181    [d, n] = fread(fid, size(1)*size(2)*2, 'float32');
182    d = reshape(complex(d(1:2:end), d(2:2:end)), size(1), size(2));
183    assignin('caller', name, d);
184  % --- dcmat ---
185  elseif (strcmp(type, 'dcmat'))
186    [size, n] = fread(fid, 2, 'uint64');
187    [d, n] = fread(fid, size(1)*size(2)*2, 'float64');
188    d = reshape(complex(d(1:2:end), d(2:2:end)), size(1), size(2));
189    assignin('caller', name, d);
190
191  % --- bArray ---
192  elseif (strcmp(type, 'bArray'))
193    [size, n] = fread(fid, 1, 'uint64');
194    [d, n] = fread(fid, size, 'char');
195    assignin('caller', name, d);
196  % --- sArray ---
197  elseif (strcmp(type, 'sArray'))
198    [size, n] = fread(fid, 1, 'uint64');
199    [d, n] = fread(fid, size, 'int16');
200    assignin('caller', name, d);
201  % --- iArray ---
202  elseif (strcmp(type, 'iArray'))
203    [size, n] = fread(fid, 1, 'uint64');
204    [d, n] = fread(fid, size, 'int32');
205    assignin('caller', name, d);
206  % --- fArray ---
207  elseif (strcmp(type, 'fArray'))
208    [size, n] = fread(fid, 1, 'uint64');
209    [d, n] = fread(fid, size, 'float32');
210    assignin('caller', name, d);
211  % --- dArray ---
212  elseif (strcmp(type, 'dArray'))
213    [size, n] = fread(fid, 1, 'uint64');
214    [d, n] = fread(fid, size, 'float64');
215    assignin('caller', name, d);
216  % --- fcArray ---
217  elseif (strcmp(type, 'fcArray'))
218    [size, n] = fread(fid, 1, 'uint64');
219    [d, n] = fread(fid, size*2, 'float32');
220    d = complex(d(1:2:end), d(2:2:end));
221    assignin('caller', name, d);
222  % --- dcArray ---
223  elseif (strcmp(type, 'dcArray'))
224    [size, n] = fread(fid, 1, 'uint64');
225    [d, n] = fread(fid, size*2, 'float64');
226    d = complex(d(1:2:end), d(2:2:end));
227    assignin('caller', name, d);
228
229  % --- bvecArray ---
230  elseif (strcmp(type, 'bvecArray'))
231    [size, n] = fread(fid, 1, 'uint64');
232    clear d2;
233    for i=1:size;
234      [size2, n] = fread(fid, 1, 'uint64');
235      [d, n] = fread(fid, size2, 'char');
236      d2{i} = d;
237    end
238    assignin('caller', name, d2);
239  % --- svecArray ---
240  elseif (strcmp(type, 'svecArray'))
241    [size, n] = fread(fid, 1, 'uint64');
242    clear d2;
243    for i=1:size;
244      [size2, n] = fread(fid, 1, 'uint64');
245      [d, n] = fread(fid, size2, 'int16');
246      d2{i} = d;
247    end
248    assignin('caller', name, d2);
249  % --- ivecArray ---
250  elseif (strcmp(type, 'ivecArray'))
251    [size, n] = fread(fid, 1, 'uint64');
252    clear d2;
253    for i=1:size;
254      [size2, n] = fread(fid, 1, 'uint64');
255      [d, n] = fread(fid, size2, 'int32');
256      d2{i} = d;
257    end
258    assignin('caller', name, d2);
259  % --- vecArray ---
260  elseif (strcmp(type, 'vecArray'))
261    [size, n] = fread(fid, 1, 'uint64');
262    clear d2;
263    for i=1:size;
264      [size2, n] = fread(fid, 1, 'uint64');
265      [d, n] = fread(fid, size2, 'float64');
266      d2{i} = d;
267    end
268    assignin('caller', name, d2);
269  % --- cvecArray ---
270  elseif (strcmp(type, 'cvecArray'))
271    [size, n] = fread(fid, 1, 'uint64');
272    clear d2;
273    for i=1:size;
274      [size2, n] = fread(fid, 1, 'uint64');
275      [d, n] = fread(fid, size*2, 'float64');
276      d2{i} = complex(d(1:2:end), d(2:2:end));
277    end
278    assignin('caller', name, d2);
279  % --- stringArray ---
280  elseif (strcmp(type, 'stringArray'))
281    [size, n] = fread(fid, 1, 'uint64');
282    clear d2;
283    for i=1:size;
284      [size2, n] = fread(fid, 1, 'uint64');
285      [d, n] = fread(fid, size2, 'char');
286      d2{i} = char(d);
287    end
288    assignin('caller', name, d2);
289
290  % --- bmatArray ---
291  elseif (strcmp(type, 'bmatArray'))
292    [size, n] = fread(fid, 1, 'uint64');
293    clear d2;
294    for i=1:size;
295      [size2, n] = fread(fid, 2, 'uint64');
296      [d, n] = fread(fid, size2', 'char');
297      d2{i} = d;
298    end
299    assignin('caller', name, d2);
300  % --- smatArray ---
301  elseif (strcmp(type, 'smatArray'))
302    [size, n] = fread(fid, 1, 'uint64');
303    clear d2;
304    for i=1:size;
305      [size2, n] = fread(fid, 2, 'uint64');
306      [d, n] = fread(fid, size2', 'int16');
307      d2{i} = d;
308    end
309    assignin('caller', name, d2);
310  % --- imatArray ---
311  elseif (strcmp(type, 'imatArray'))
312    [size, n] = fread(fid, 1, 'uint64');
313    clear d2;
314    for i=1:size;
315      [size2, n] = fread(fid, 2, 'uint64');
316      [d, n] = fread(fid, size2', 'int32');
317      d2{i} = d;
318    end
319    assignin('caller', name, d2);
320  % --- matArray ---
321  elseif (strcmp(type, 'matArray'))
322    [size, n] = fread(fid, 1, 'uint64');
323    clear d2;
324    for i=1:size;
325      [size2, n] = fread(fid, 2, 'uint64');
326      [d, n] = fread(fid, size2', 'float64');
327      d2{i} = d;
328    end
329    assignin('caller', name, d2);
330  % --- cmatArray ---
331  elseif (strcmp(type, 'cmatArray'))
332    [size, n] = fread(fid, 1, 'uint64');
333    clear d2;
334    for i=1:size;
335      [size2, n] = fread(fid, 2, 'uint64');
336      [d, n] = fread(fid, size2(1)*size2(2)*2, 'float64');
337      d2{i} = reshape(complex(d(1:2:end), d(2:2:end)), size2(1), size2(2));
338    end
339    assignin('caller', name, d2);
340
341  % --- else ---
342  else
343    warning(['Not supported type: ' type]);
344  end
345
346  if (p + d1(3) >= file_size)
347    break;
348  else
349    fseek(fid, p+d1(3), 'bof');
350  end
351
352end
353
354fclose(fid);
355
356
357
358function str = fgetstr(fid)
359str = '';
360while (1)
361  [d, n] = fread(fid, 1, 'char');
362  if (d == 0)
363    break;
364  end
365  str = [str char(d)];
366end
Note: See TracBrowser for help on using the browser.