1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of hdrload</title> |
---|
6 | <meta name="keywords" content="hdrload"> |
---|
7 | <meta name="description" content="HDRLOAD Load data from an ASCII file containing a text header."> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../../index.html">Home</a> > <a href="#">applications</a> > <a href="index.html">common</a> > hdrload.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for applications/common <img alt=">" border="0" src="../../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>hdrload |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
24 | <div class="box"><strong>HDRLOAD Load data from an ASCII file containing a text header.</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function [header, data] = hdrload(file) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment"> HDRLOAD Load data from an ASCII file containing a text header. |
---|
31 | [header, data] = HDRLOAD('filename.ext') reads a data file |
---|
32 | called 'filename.ext', which contains a text header. There |
---|
33 | is no default extension; any extensions must be explicitly |
---|
34 | supplied. |
---|
35 | |
---|
36 | The first output, HEADER, is the header information, |
---|
37 | returned as a text array. |
---|
38 | The second output, DATA, is the data matrix. This data |
---|
39 | matrix has the same dimensions as the data in the file, one |
---|
40 | row per line of ASCII data in the file. If the data is not |
---|
41 | regularly spaced (i.e., each line of ASCII data does not |
---|
42 | contain the same number of points), the data is returned as |
---|
43 | a column vector. |
---|
44 | |
---|
45 | Limitations: No line of the text header can begin with |
---|
46 | a number. Only one header and data set will be read, |
---|
47 | and the header must come before the data. |
---|
48 | |
---|
49 | See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT. |
---|
50 | See also the IOFUN directory.</pre></div> |
---|
51 | |
---|
52 | <!-- crossreference --> |
---|
53 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
54 | This function calls: |
---|
55 | <ul style="list-style-image:url(../../matlabicon.gif)"> |
---|
56 | </ul> |
---|
57 | This function is called by: |
---|
58 | <ul style="list-style-image:url(../../matlabicon.gif)"> |
---|
59 | </ul> |
---|
60 | <!-- crossreference --> |
---|
61 | |
---|
62 | |
---|
63 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
64 | <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [header, data] = hdrload(file)</a> |
---|
65 | 0002 |
---|
66 | 0003 <span class="comment">% HDRLOAD Load data from an ASCII file containing a text header.</span> |
---|
67 | 0004 <span class="comment">% [header, data] = HDRLOAD('filename.ext') reads a data file</span> |
---|
68 | 0005 <span class="comment">% called 'filename.ext', which contains a text header. There</span> |
---|
69 | 0006 <span class="comment">% is no default extension; any extensions must be explicitly</span> |
---|
70 | 0007 <span class="comment">% supplied.</span> |
---|
71 | 0008 <span class="comment">%</span> |
---|
72 | 0009 <span class="comment">% The first output, HEADER, is the header information,</span> |
---|
73 | 0010 <span class="comment">% returned as a text array.</span> |
---|
74 | 0011 <span class="comment">% The second output, DATA, is the data matrix. This data</span> |
---|
75 | 0012 <span class="comment">% matrix has the same dimensions as the data in the file, one</span> |
---|
76 | 0013 <span class="comment">% row per line of ASCII data in the file. If the data is not</span> |
---|
77 | 0014 <span class="comment">% regularly spaced (i.e., each line of ASCII data does not</span> |
---|
78 | 0015 <span class="comment">% contain the same number of points), the data is returned as</span> |
---|
79 | 0016 <span class="comment">% a column vector.</span> |
---|
80 | 0017 <span class="comment">%</span> |
---|
81 | 0018 <span class="comment">% Limitations: No line of the text header can begin with</span> |
---|
82 | 0019 <span class="comment">% a number. Only one header and data set will be read,</span> |
---|
83 | 0020 <span class="comment">% and the header must come before the data.</span> |
---|
84 | 0021 <span class="comment">%</span> |
---|
85 | 0022 <span class="comment">% See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT.</span> |
---|
86 | 0023 <span class="comment">% See also the IOFUN directory.</span> |
---|
87 | 0024 |
---|
88 | 0025 <span class="comment">% check number and type of arguments</span> |
---|
89 | 0026 <span class="keyword">if</span> nargin < 1 |
---|
90 | 0027 error(<span class="string">'Function requires one input argument'</span>); |
---|
91 | 0028 <span class="keyword">elseif</span> ~isstr(file) |
---|
92 | 0029 error(<span class="string">'Input must be a string representing a filename'</span>); |
---|
93 | 0030 <span class="keyword">end</span> |
---|
94 | 0031 |
---|
95 | 0032 <span class="comment">% Open the file. If this returns a -1, we did not open the file</span> |
---|
96 | 0033 <span class="comment">% successfully.</span> |
---|
97 | 0034 fid = fopen(file); |
---|
98 | 0035 <span class="keyword">if</span> fid==-1 |
---|
99 | 0036 error(<span class="string">'File not found or permission denied'</span>); |
---|
100 | 0037 <span class="keyword">end</span> |
---|
101 | 0038 |
---|
102 | 0039 <span class="comment">% Initialize loop variables</span> |
---|
103 | 0040 <span class="comment">% We store the number of lines in the header, and the maximum</span> |
---|
104 | 0041 <span class="comment">% length of any one line in the header. These are used later</span> |
---|
105 | 0042 <span class="comment">% in assigning the 'header' output variable.</span> |
---|
106 | 0043 no_lines = 0; |
---|
107 | 0044 max_line = 0; |
---|
108 | 0045 |
---|
109 | 0046 <span class="comment">% We also store the number of columns in the data we read. This</span> |
---|
110 | 0047 <span class="comment">% way we can compute the size of the output based on the number</span> |
---|
111 | 0048 <span class="comment">% of columns and the total number of data points.</span> |
---|
112 | 0049 ncols = 0; |
---|
113 | 0050 |
---|
114 | 0051 <span class="comment">% Finally, we initialize the data to [].</span> |
---|
115 | 0052 data = []; |
---|
116 | 0053 |
---|
117 | 0054 <span class="comment">% Start processing.</span> |
---|
118 | 0055 line = fgetl(fid); |
---|
119 | 0056 <span class="keyword">if</span> ~isstr(line) |
---|
120 | 0057 disp(<span class="string">'Warning: file contains no header and no data'</span>) |
---|
121 | 0058 <span class="keyword">end</span>; |
---|
122 | 0059 [data, ncols, errmsg, nxtindex] = sscanf(line, <span class="string">'%f'</span>); |
---|
123 | 0060 |
---|
124 | 0061 <span class="comment">% One slight problem, pointed out by Peter vanderWal: If the</span> |
---|
125 | 0062 <span class="comment">% first character of the line is 'e', then this will scan as</span> |
---|
126 | 0063 <span class="comment">% 0.00e+00. We can trap this case specifically by using the</span> |
---|
127 | 0064 <span class="comment">% 'next index' output: in the case of a stripped 'e' the next</span> |
---|
128 | 0065 <span class="comment">% index is one, indicating zero characters read. See the help</span> |
---|
129 | 0066 <span class="comment">% entry for 'sscanf' for more information on this output</span> |
---|
130 | 0067 <span class="comment">% parameter. We loop through the file one line at a time until</span> |
---|
131 | 0068 <span class="comment">% we find some data. After that point we stop checking for</span> |
---|
132 | 0069 <span class="comment">% header information. This part of the program takes most of the</span> |
---|
133 | 0070 <span class="comment">% processing time, because fgetl is relatively slow (compared to</span> |
---|
134 | 0071 <span class="comment">% fscanf, which we will use later).</span> |
---|
135 | 0072 <span class="keyword">while</span> isempty(data)|(nxtindex==1) |
---|
136 | 0073 no_lines = no_lines+1; |
---|
137 | 0074 max_line = max([max_line, length(line)]); |
---|
138 | 0075 <span class="comment">% Create unique variable to hold this line of text information.</span> |
---|
139 | 0076 <span class="comment">% Store the last-read line in this variable.</span> |
---|
140 | 0077 eval([<span class="string">'line'</span>, num2str(no_lines), <span class="string">'=line;'</span>]); |
---|
141 | 0078 line = fgetl(fid); |
---|
142 | 0079 <span class="keyword">if</span> ~isstr(line) |
---|
143 | 0080 disp(<span class="string">'Warning: file contains no data'</span>) |
---|
144 | 0081 <span class="keyword">break</span> |
---|
145 | 0082 <span class="keyword">end</span>; |
---|
146 | 0083 [data, ncols, errmsg, nxtindex] = sscanf(line, <span class="string">'%f'</span>); |
---|
147 | 0084 <span class="keyword">end</span> <span class="comment">% while</span> |
---|
148 | 0085 |
---|
149 | 0086 <span class="comment">% Now that we have read in the first line of data, we can skip</span> |
---|
150 | 0087 <span class="comment">% the processing that stores header information, and just read</span> |
---|
151 | 0088 <span class="comment">% in the rest of the data.</span> |
---|
152 | 0089 data = [data; fscanf(fid, <span class="string">'%f'</span>)]; |
---|
153 | 0090 fclose(fid); |
---|
154 | 0091 |
---|
155 | 0092 <span class="comment">% Create header output from line information. The number of lines</span> |
---|
156 | 0093 <span class="comment">% and the maximum line length are stored explicitly, and each</span> |
---|
157 | 0094 <span class="comment">% line is stored in a unique variable using the 'eval' statement</span> |
---|
158 | 0095 <span class="comment">% within the loop. Note that, if we knew a priori that the</span> |
---|
159 | 0096 <span class="comment">% headers were 10 lines or less, we could use the STR2MAT</span> |
---|
160 | 0097 <span class="comment">% function and save some work. First, initialize the header to an</span> |
---|
161 | 0098 <span class="comment">% array of spaces.</span> |
---|
162 | 0099 header = setstr(<span class="string">' '</span>*ones(no_lines, max_line)); |
---|
163 | 0100 <span class="keyword">for</span> i = 1:no_lines |
---|
164 | 0101 varname = [<span class="string">'line'</span> num2str(i)]; |
---|
165 | 0102 <span class="comment">% Note that we only assign this line variable to a subset of</span> |
---|
166 | 0103 <span class="comment">% this row of the header array. We thus ensure that the matrix</span> |
---|
167 | 0104 <span class="comment">% sizes in the assignment are equal.</span> |
---|
168 | 0105 eval([<span class="string">'header(i, 1:length('</span> varname <span class="string">')) = '</span> varname <span class="string">';'</span>]); |
---|
169 | 0106 <span class="keyword">end</span> |
---|
170 | 0107 |
---|
171 | 0108 <span class="comment">% Resize output data, based on the number of columns (as returned</span> |
---|
172 | 0109 <span class="comment">% from the sscanf of the first line of data) and the total number</span> |
---|
173 | 0110 <span class="comment">% of data elements. Since the data was read in row-wise, and</span> |
---|
174 | 0111 <span class="comment">% MATLAB stores data in columnwise format, we have to reverse the</span> |
---|
175 | 0112 <span class="comment">% size arguments and then transpose the data. If we read in</span> |
---|
176 | 0113 <span class="comment">% irregularly spaced data, then the division we are about to do</span> |
---|
177 | 0114 <span class="comment">% will not work. Therefore, we will trap the error with an EVAL</span> |
---|
178 | 0115 <span class="comment">% call; if the reshape fails, we will just return the data as is.</span> |
---|
179 | 0116 eval(<span class="string">'data = reshape(data, ncols, length(data)/ncols)'';'</span>, <span class="string">''</span>);</pre></div> |
---|
180 | <hr><address>Generated on Mon 21-May-2007 15:32:41 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
181 | </body> |
---|
182 | </html> |
---|