ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
numlines.m
Go to the documentation of this file.
1%> \brief
2%> Return a scalar MATLAB integer representing
3%> the number of lines in the specified input ``idname``.
4%>
5%> \param[in] idname : The input scalar MATLAB object that can be either:<br>
6%> <ol>
7%> <li> a MATLAB string containing the path
8%> to a record-based external file whose
9%> number of records (lines) must be returned.<br>
10%> <li> a MATLAB integer, representing the file
11%> ID of an opened record-based file whose
12%> number of records (lines) must be returned.<br>
13%> </ol>
14%>
15%> \return
16%> ``nlines`` : The output scalar MATLAB integer containing the
17%> number of records (lines) in the specified input ``idname``.
18%>
19%> \interface{numlines}
20%> \code{.m}
21%>
22%> nlines = pm.io.numlines(idname);
23%>
24%> \endcode
25%>
26%> \warning
27%> The input file (ID) will be closed even if it is open upon entry to the function.
28%>
29%> \example{numlines}
30%> \include{lineno} example/io/numlines/main.m
31%> \output{numlines}
32%> \include{lineno} example/io/numlines/main.out.m
33%>
34%> \final{numlines}
35%>
36%> \author
37%> \JoshuaOsborne, May 21 2024, 5:50 PM, University of Texas at Arlington<br>
38%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
39%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
40function nlines = numlines(idname)
41 if pm.introspection.istype(idname, "string", 1)
42 [fid, errmsg] = fopen(idname, 'r');
43 if fid < 0
44 help("pm.io.numlines");
45 error( newline + ...
46 + "Failed to open file """ + idname + """: " + newline ...
47 + newline ...
48 + string(errmsg) + newline ...
49 + newline ...
50 + "For more information, see the documentation of the function displayed above" + newline ...
51 + newline ...
52 );
53 end
54 elseif pm.introspection.istype(idname, "integer", 1)
55 fid = idname;
56 else
57 help("pm.io.numlines");
58 disp("idname = ");
59 disp(idname);
60 error ( newline ...
61 + "Invalid input value for ``idname`` displayed above." + newline ...
62 + "For more information, see the documentation of the function displayed above" + newline ...
63 + newline ...
64 );
65 end
66 nlines = 0;
67 while true
68 line = fgetl(fid);
69 if ~ischar(line)
70 break;
71 else
72 nlines = nlines + 1;
73 end
74 end
75 fclose(fid);
76end
function numlines(in idname)
Return a scalar MATLAB integer representing the number of lines in the specified input idname.