source: MML/trunk/applications/doc_html/applications/common/hdrload.html @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 11.1 KB
Line 
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 &copy; 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> &gt;  <a href="#">applications</a> &gt; <a href="index.html">common</a> &gt; hdrload.m</div>
16
17<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
18<td align="right"><a href="index.html">Index for applications/common&nbsp;<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>
54This function calls:
55<ul style="list-style-image:url(../../matlabicon.gif)">
56</ul>
57This 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>
650002
660003 <span class="comment">% HDRLOAD Load data from an ASCII file containing a text header.</span>
670004 <span class="comment">%     [header, data] = HDRLOAD('filename.ext') reads a data file</span>
680005 <span class="comment">%     called 'filename.ext', which contains a text header.  There</span>
690006 <span class="comment">%     is no default extension; any extensions must be explicitly</span>
700007 <span class="comment">%     supplied.</span>
710008 <span class="comment">%</span>
720009 <span class="comment">%     The first output, HEADER, is the header information,</span>
730010 <span class="comment">%     returned as a text array.</span>
740011 <span class="comment">%     The second output, DATA, is the data matrix.  This data</span>
750012 <span class="comment">%     matrix has the same dimensions as the data in the file, one</span>
760013 <span class="comment">%     row per line of ASCII data in the file.  If the data is not</span>
770014 <span class="comment">%     regularly spaced (i.e., each line of ASCII data does not</span>
780015 <span class="comment">%     contain the same number of points), the data is returned as</span>
790016 <span class="comment">%     a column vector.</span>
800017 <span class="comment">%</span>
810018 <span class="comment">%     Limitations:  No line of the text header can begin with</span>
820019 <span class="comment">%     a number.  Only one header and data set will be read,</span>
830020 <span class="comment">%     and the header must come before the data.</span>
840021 <span class="comment">%</span>
850022 <span class="comment">%     See also LOAD, SAVE, SPCONVERT, FSCANF, FPRINTF, STR2MAT.</span>
860023 <span class="comment">%     See also the IOFUN directory.</span>
870024
880025 <span class="comment">% check number and type of arguments</span>
890026 <span class="keyword">if</span> nargin &lt; 1
900027   error(<span class="string">'Function requires one input argument'</span>);
910028 <span class="keyword">elseif</span> ~isstr(file)
920029   error(<span class="string">'Input must be a string representing a filename'</span>);
930030 <span class="keyword">end</span>
940031
950032 <span class="comment">% Open the file.  If this returns a -1, we did not open the file</span>
960033 <span class="comment">% successfully.</span>
970034 fid = fopen(file);
980035 <span class="keyword">if</span> fid==-1
990036   error(<span class="string">'File not found or permission denied'</span>);
1000037   <span class="keyword">end</span>
1010038
1020039 <span class="comment">% Initialize loop variables</span>
1030040 <span class="comment">% We store the number of lines in the header, and the maximum</span>
1040041 <span class="comment">% length of any one line in the header.  These are used later</span>
1050042 <span class="comment">% in assigning the 'header' output variable.</span>
1060043 no_lines = 0;
1070044 max_line = 0;
1080045
1090046 <span class="comment">% We also store the number of columns in the data we read.  This</span>
1100047 <span class="comment">% way we can compute the size of the output based on the number</span>
1110048 <span class="comment">% of columns and the total number of data points.</span>
1120049 ncols = 0;
1130050
1140051 <span class="comment">% Finally, we initialize the data to [].</span>
1150052 data = [];
1160053
1170054 <span class="comment">% Start processing.</span>
1180055 line = fgetl(fid);
1190056 <span class="keyword">if</span> ~isstr(line)
1200057   disp(<span class="string">'Warning: file contains no header and no data'</span>)
1210058   <span class="keyword">end</span>;
1220059 [data, ncols, errmsg, nxtindex] = sscanf(line, <span class="string">'%f'</span>);
1230060
1240061 <span class="comment">% One slight problem, pointed out by Peter vanderWal: If the</span>
1250062 <span class="comment">% first character of the line is 'e', then this will scan as</span>
1260063 <span class="comment">% 0.00e+00. We can trap this case specifically by using the</span>
1270064 <span class="comment">% 'next index' output: in the case of a stripped 'e' the next</span>
1280065 <span class="comment">% index is one, indicating zero characters read.  See the help</span>
1290066 <span class="comment">% entry for 'sscanf' for more information on this output</span>
1300067 <span class="comment">% parameter. We loop through the file one line at a time until</span>
1310068 <span class="comment">% we find some data.  After that point we stop checking for</span>
1320069 <span class="comment">% header information. This part of the program takes most of the</span>
1330070 <span class="comment">% processing time, because fgetl is relatively slow (compared to</span>
1340071 <span class="comment">% fscanf, which we will use later).</span>
1350072 <span class="keyword">while</span> isempty(data)|(nxtindex==1)
1360073   no_lines = no_lines+1;
1370074   max_line = max([max_line, length(line)]);
1380075   <span class="comment">% Create unique variable to hold this line of text information.</span>
1390076   <span class="comment">% Store the last-read line in this variable.</span>
1400077   eval([<span class="string">'line'</span>, num2str(no_lines), <span class="string">'=line;'</span>]);
1410078   line = fgetl(fid);
1420079   <span class="keyword">if</span> ~isstr(line)
1430080     disp(<span class="string">'Warning: file contains no data'</span>)
1440081     <span class="keyword">break</span>
1450082     <span class="keyword">end</span>;
1460083   [data, ncols, errmsg, nxtindex] = sscanf(line, <span class="string">'%f'</span>);
1470084   <span class="keyword">end</span> <span class="comment">% while</span>
1480085
1490086 <span class="comment">% Now that we have read in the first line of data, we can skip</span>
1500087 <span class="comment">% the processing that stores header information, and just read</span>
1510088 <span class="comment">% in the rest of the data.</span>
1520089 data = [data; fscanf(fid, <span class="string">'%f'</span>)];
1530090 fclose(fid);
1540091
1550092 <span class="comment">% Create header output from line information. The number of lines</span>
1560093 <span class="comment">% and the maximum line length are stored explicitly, and each</span>
1570094 <span class="comment">% line is stored in a unique variable using the 'eval' statement</span>
1580095 <span class="comment">% within the loop. Note that, if we knew a priori that the</span>
1590096 <span class="comment">% headers were 10 lines or less, we could use the STR2MAT</span>
1600097 <span class="comment">% function and save some work. First, initialize the header to an</span>
1610098 <span class="comment">% array of spaces.</span>
1620099 header = setstr(<span class="string">' '</span>*ones(no_lines, max_line));
1630100 <span class="keyword">for</span> i = 1:no_lines
1640101   varname = [<span class="string">'line'</span> num2str(i)];
1650102   <span class="comment">% Note that we only assign this line variable to a subset of</span>
1660103   <span class="comment">% this row of the header array.  We thus ensure that the matrix</span>
1670104   <span class="comment">% sizes in the assignment are equal.</span>
1680105   eval([<span class="string">'header(i, 1:length('</span> varname <span class="string">')) = '</span> varname <span class="string">';'</span>]);
1690106   <span class="keyword">end</span>
1700107
1710108 <span class="comment">% Resize output data, based on the number of columns (as returned</span>
1720109 <span class="comment">% from the sscanf of the first line of data) and the total number</span>
1730110 <span class="comment">% of data elements. Since the data was read in row-wise, and</span>
1740111 <span class="comment">% MATLAB stores data in columnwise format, we have to reverse the</span>
1750112 <span class="comment">% size arguments and then transpose the data.  If we read in</span>
1760113 <span class="comment">% irregularly spaced data, then the division we are about to do</span>
1770114 <span class="comment">% will not work. Therefore, we will trap the error with an EVAL</span>
1780115 <span class="comment">% call; if the reshape fails, we will just return the data as is.</span>
1790116 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> &copy; 2003</address>
181</body>
182</html>
Note: See TracBrowser for help on using the repository browser.