1 |
function [header, data] = hdrload(file) |
---|
2 |
|
---|
3 |
% HDRLOAD Load data from an ASCII file containing a text header. |
---|
4 |
% [header, data] = HDRLOAD('filename.ext') reads a data file |
---|
5 |
% called 'filename.ext', which contains a text header. There |
---|
6 |
% is no default extension; any extensions must be explicitly |
---|
7 |
% supplied. |
---|
8 |
% |
---|
9 |
% The first output, HEADER, is the header information, returned |
---|
10 |
% as a text array. |
---|
11 |
% The second output, DATA, is the data matrix. This data matrix |
---|
12 |
% has the same dimensions as the data in the file, one row per |
---|
13 |
% line of ASCII data in the file. If the data is not regularly |
---|
14 |
% spaced (i.e., each line of ASCII data does not contain the |
---|
15 |
% same number of points), the data is returned as a column |
---|
16 |
% vector. |
---|
17 |
% |
---|
18 |
% Limitations: No line of the text header can begin with |
---|
19 |
% a number. Only one header and data set will be read, |
---|
20 |
% and the header must come before the data. |
---|
21 |
% |
---|
22 |
% See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT. |
---|
23 |
% See also the IOFUN directory. |
---|
24 |
|
---|
25 |
% check number and type of arguments |
---|
26 |
if nargin < 1 |
---|
27 |
error('Function requires one input argument'); |
---|
28 |
elseif ~isstr(file) |
---|
29 |
error('Input argument must be a string representing a filename'); |
---|
30 |
end |
---|
31 |
|
---|
32 |
% Open the file. If this returns a -1, we did not open the file |
---|
33 |
% successfully. |
---|
34 |
fid = fopen(file); |
---|
35 |
if fid==-1 |
---|
36 |
error('File not found or permission denied'); |
---|
37 |
end |
---|
38 |
|
---|
39 |
% Initialize loop variables |
---|
40 |
% We store the number of lines in the header, and the maximum length |
---|
41 |
% of any one line in the header. These are used later in assigning |
---|
42 |
% the 'header' output variable. |
---|
43 |
no_lines = 0; |
---|
44 |
max_line = 0; |
---|
45 |
|
---|
46 |
% We also store the number of columns in the data we read. This way |
---|
47 |
% we can compute the size of the output based on the number of |
---|
48 |
% columns and the total number of data points. |
---|
49 |
ncols = 0; |
---|
50 |
|
---|
51 |
% Finally, we initialize the data to []. |
---|
52 |
data = []; |
---|
53 |
|
---|
54 |
% Start processing. |
---|
55 |
line = fgetl(fid); |
---|
56 |
if ~isstr(line) |
---|
57 |
disp('Warning: file contains no header and no data') |
---|
58 |
end; |
---|
59 |
[data, ncols] = sscanf(line, '%f'); |
---|
60 |
|
---|
61 |
% We loop through the file one line at a time until we find some |
---|
62 |
% data. After that point we stop checking for header information. |
---|
63 |
% This part of the program takes most of the processing time, because |
---|
64 |
% fgetl is relatively slow (compared to fscanf, which we will use |
---|
65 |
% later). |
---|
66 |
while isempty(data) |
---|
67 |
no_lines = no_lines+1; |
---|
68 |
max_line = max([max_line, length(line)]); |
---|
69 |
% Create unique variable to hold this line of text information. |
---|
70 |
% Store the last-read line in this variable. |
---|
71 |
eval(['line', num2str(no_lines), '=line;']); |
---|
72 |
line = fgetl(fid); |
---|
73 |
if ~isstr(line) |
---|
74 |
disp('Warning: file contains no data') |
---|
75 |
break |
---|
76 |
end; |
---|
77 |
[data, ncols] = sscanf(line, '%f'); |
---|
78 |
end % while |
---|
79 |
|
---|
80 |
% Now that we have read in the first line of data, we can skip the |
---|
81 |
% processing that stores header information, and just read in the |
---|
82 |
% rest of the data. |
---|
83 |
data = [data; fscanf(fid, '%f')]; |
---|
84 |
fclose(fid); |
---|
85 |
|
---|
86 |
% Create header output from line information. The number of lines and |
---|
87 |
% the maximum line length are stored explicitly, and each line is |
---|
88 |
% stored in a unique variable using the 'eval' statement within the |
---|
89 |
% loop. Note that, if we knew a priori that the headers were 10 lines |
---|
90 |
% or less, we could use the STR2MAT function and save some work. |
---|
91 |
% First, initialize the header to an array of spaces. |
---|
92 |
header = setstr(' '*ones(no_lines, max_line)); |
---|
93 |
for i = 1:no_lines |
---|
94 |
varname = ['line' num2str(i)]; |
---|
95 |
% Note that we only assign this line variable to a subset of this |
---|
96 |
% row of the header array. We thus ensure that the matrix sizes in |
---|
97 |
% the assignment are equal. |
---|
98 |
eval(['header(i, 1:length(' varname ')) = ' varname ';']); |
---|
99 |
end |
---|
100 |
|
---|
101 |
% Resize output data, based on the number of columns (as returned |
---|
102 |
% from the sscanf of the first line of data) and the total number of |
---|
103 |
% data elements. Since the data was read in row-wise, and MATLAB |
---|
104 |
% stores data in columnwise format, we have to reverse the size |
---|
105 |
% arguments and then transpose the data. If we read in irregularly |
---|
106 |
% spaced data, then the division we are about to do will not work. |
---|
107 |
% Therefore, we will trap the error with an EVAL call; if the reshape |
---|
108 |
% fails, we will just return the data as is. |
---|
109 |
eval('data = reshape(data, ncols, length(data)/ncols)'';', ''); |
---|
110 |
|
---|
111 |
% And we're done! |
---|