Home > custom > io > hdrload.m

hdrload

PURPOSE ^

HDRLOAD Load data from an ASCII file containing a text header.

SYNOPSIS ^

function [header, data] = hdrload(file)

DESCRIPTION ^

 HDRLOAD Load data from an ASCII file containing a text header.
     [header, data] = HDRLOAD('filename.ext') reads a data file
     called 'filename.ext', which contains a text header.  There
     is no default extension; any extensions must be explicitly
     supplied.

     The first output, HEADER, is the header information, returned
     as a text array.
     The second output, DATA, is the data matrix.  This data matrix
     has the same dimensions as the data in the file, one row per
     line of ASCII data in the file.  If the data is not regularly
     spaced (i.e., each line of ASCII data does not contain the
     same number of points), the data is returned as a column
     vector.

     Limitations:  No line of the text header can begin with
     a number.  Only one header and data set will be read,
     and the header must come before the data.

     See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT.
     See also the IOFUN directory.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [header, data] = hdrload(file)
0002 
0003 % HDRLOAD Load data from an ASCII file containing a text header.
0004 %     [header, data] = HDRLOAD('filename.ext') reads a data file
0005 %     called 'filename.ext', which contains a text header.  There
0006 %     is no default extension; any extensions must be explicitly
0007 %     supplied.
0008 %
0009 %     The first output, HEADER, is the header information, returned
0010 %     as a text array.
0011 %     The second output, DATA, is the data matrix.  This data matrix
0012 %     has the same dimensions as the data in the file, one row per
0013 %     line of ASCII data in the file.  If the data is not regularly
0014 %     spaced (i.e., each line of ASCII data does not contain the
0015 %     same number of points), the data is returned as a column
0016 %     vector.
0017 %
0018 %     Limitations:  No line of the text header can begin with
0019 %     a number.  Only one header and data set will be read,
0020 %     and the header must come before the data.
0021 %
0022 %     See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT.
0023 %     See also the IOFUN directory.
0024 
0025 % check number and type of arguments
0026 if nargin < 1
0027   error('Function requires one input argument');
0028 elseif ~isstr(file)
0029   error('Input argument must be a string representing a filename');
0030 end
0031 
0032 % Open the file.  If this returns a -1, we did not open the file
0033 % successfully.
0034 fid = fopen(file);
0035 if fid==-1
0036   error('File not found or permission denied');
0037   end
0038 
0039 % Initialize loop variables
0040 % We store the number of lines in the header, and the maximum length
0041 % of any one line in the header.  These are used later in assigning
0042 % the 'header' output variable.
0043 no_lines = 0;
0044 max_line = 0;
0045 
0046 % We also store the number of columns in the data we read.  This way
0047 % we can compute the size of the output based on the number of
0048 % columns and the total number of data points.
0049 ncols = 0;
0050 
0051 % Finally, we initialize the data to [].
0052 data = [];
0053 
0054 % Start processing.
0055 line = fgetl(fid);
0056 if ~isstr(line)
0057   disp('Warning: file contains no header and no data')
0058   end;
0059 [data, ncols] = sscanf(line, '%f');
0060 
0061 % We loop through the file one line at a time until we find some
0062 % data.  After that point we stop checking for header information.
0063 % This part of the program takes most of the processing time, because
0064 % fgetl is relatively slow (compared to fscanf, which we will use
0065 % later).
0066 while isempty(data)
0067   no_lines = no_lines+1;
0068   max_line = max([max_line, length(line)]);
0069   % Create unique variable to hold this line of text information.
0070   % Store the last-read line in this variable.
0071   eval(['line', num2str(no_lines), '=line;']);
0072   line = fgetl(fid);
0073   if ~isstr(line)
0074     disp('Warning: file contains no data')
0075     break
0076     end;
0077   [data, ncols] = sscanf(line, '%f');
0078   end % while
0079 
0080 % Now that we have read in the first line of data, we can skip the
0081 % processing that stores header information, and just read in the
0082 % rest of the data.
0083 data = [data; fscanf(fid, '%f')];
0084 fclose(fid);
0085 
0086 % Create header output from line information. The number of lines and
0087 % the maximum line length are stored explicitly, and each line is
0088 % stored in a unique variable using the 'eval' statement within the
0089 % loop. Note that, if we knew a priori that the headers were 10 lines
0090 % or less, we could use the STR2MAT function and save some work.
0091 % First, initialize the header to an array of spaces.
0092 header = setstr(' '*ones(no_lines, max_line));
0093 for i = 1:no_lines
0094   varname = ['line' num2str(i)];
0095   % Note that we only assign this line variable to a subset of this
0096   % row of the header array.  We thus ensure that the matrix sizes in
0097   % the assignment are equal.
0098   eval(['header(i, 1:length(' varname ')) = ' varname ';']);
0099   end
0100 
0101 % Resize output data, based on the number of columns (as returned
0102 % from the sscanf of the first line of data) and the total number of
0103 % data elements. Since the data was read in row-wise, and MATLAB
0104 % stores data in columnwise format, we have to reverse the size
0105 % arguments and then transpose the data.  If we read in irregularly
0106 % spaced data, then the division we are about to do will not work.
0107 % Therefore, we will trap the error with an EVAL call; if the reshape
0108 % fails, we will just return the data as is.
0109 eval('data = reshape(data, ncols, length(data)/ncols)'';', '');
0110 
0111 % And we're done!

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003