Mercurial > hg > octave-lyh
annotate scripts/io/textscan.m @ 11191:01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 04 Nov 2010 12:18:08 -0700 |
parents | 224c80da37c5 |
children | c776f063fefe |
rev | line source |
---|---|
11141 | 1 ## Copyright (C) 2010 Ben Abbott <bpabbott@mac.com> |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{c} =} textscan (@var{fid}, @var{format}) | |
21 ## @deftypefnx {Function File} {@var{c} =} textscan (@var{fid}, @var{format}, @ | |
22 ## @var{n}) | |
23 ## @deftypefnx {Function File} {@var{c} =} textscan (@var{fid}, @var{format}, @ | |
24 ## @var{param}, @var{value}, @dots{}) | |
25 ## @deftypefnx {Function File} {@var{c} =} textscan (@var{fid}, @var{format}, @ | |
26 ## @var{n}, @var{param}, @var{value}, @dots{}) | |
27 ## @deftypefnx {Function File} {@var{a} =} textscan (@var{str}, @dots{}) | |
28 ## @deftypefnx {Function File} {[@var{a}, @var{position}] =} textscan (@dots{}) | |
29 ## Read data from a text file. | |
30 ## | |
31 ## The file associated with @var{fid} is read and parsed according to @var{format}. | |
32 ## The function behaves like @code{strread} except it works by parsing a file | |
33 ## instead of a string. See the documentation of @code{strread} for details. | |
34 ## In addition to the options supported by @code{strread}, this function | |
35 ## supports one more: | |
36 ## @itemize | |
37 ## @item "headerlines": | |
38 ## @end itemize | |
39 ## The first @var{value} number of lines of @var{str} are skipped. | |
40 ## | |
41 ## The optional input, @var{n}, specifes the number of lines to be read from | |
42 ## the file, associated with @var{fid}. | |
43 ## | |
44 ## The output, @var{c}, is a cell array whose length is given by the number | |
45 ## of format specifiers. | |
46 ## | |
47 ## The second output, @var{position}, provides the position, in characters, | |
48 ## from the beginning of the file. | |
49 ## | |
50 ## @seealso{dlmread, fscanf, load, strread, textread} | |
51 ## @end deftypefn | |
52 | |
53 function [c, p] = textscan (fid, formatstr, varargin) | |
54 | |
55 ## Check input | |
56 if (nargin < 1) | |
57 print_usage (); | |
58 elseif (nargin == 1 || isempty (formatstr)) | |
59 formatstr = "%f"; | |
60 endif | |
61 | |
62 if (nargin > 2 && isnumeric (varargin{1})) | |
63 nlines = varargin{1}; | |
64 args = varargin(2:end); | |
65 else | |
66 nlines = Inf; | |
67 args = varargin; | |
68 endif | |
69 | |
70 if (! any (strcmpi (args, "emptyvalue"))) | |
71 ## Matlab returns NaNs for missing values | |
72 args{end+1} = "emptyvalue"; | |
73 args{end+1} = NaN; | |
74 endif | |
75 | |
76 if (isa (fid, "double") && fid > 0 || ischar (fid)) | |
77 if (ischar (formatstr)) | |
78 if (ischar (fid)) | |
79 if (nargout == 2) | |
80 error ("textscan: cannot provide postion information for character input") | |
81 endif | |
82 str = fid; | |
83 else | |
84 ## Maybe skip header lines | |
85 headerlines = find (strcmpi (args, "headerlines"), 1); | |
86 if (! isempty (headerlines)) | |
87 fskipl (fid, headerlines); | |
88 args(headerlines:headerlines+1) = []; | |
89 endif | |
90 if (isfinite (nlines)) | |
91 str = ""; | |
92 for n = 1:nlines | |
93 str = strcat (str, fgets (fid)); | |
94 endfor | |
95 else | |
96 str = fread (fid, "char=>char").'; | |
97 endif | |
98 endif | |
99 | |
100 ## Determine the number of data fields | |
101 num_fields = numel (strfind (formatstr, "%")) - ... | |
102 numel (idx_star = strfind (formatstr, "%*")); | |
103 | |
104 ## Call strread to make it do the real work | |
105 c = cell (1, num_fields); | |
106 [c{:}] = strread (str, formatstr, args{:}); | |
107 | |
108 if (ischar (fid) && isfinite (nlines)) | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11141
diff
changeset
|
109 c = cellfun (@(x) x(1:nlines), c, "uniformoutput", false); |
11141 | 110 endif |
111 | |
112 if (nargout == 2) | |
113 p = ftell (fid); | |
114 endif | |
115 | |
116 else | |
117 error ("textscan: second input must be a format specification"); | |
118 endif | |
119 else | |
120 error ("textscan: expecting first argument to be a file id or character string"); | |
121 endif | |
122 | |
123 endfunction | |
124 | |
125 %!test | |
126 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12"; | |
127 %! fmtstr = "%f %d %f %s"; | |
128 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf); | |
129 %! assert (isequal (c{1}, [1;5])) | |
130 %! assert (length (c{1}), 2); | |
131 %! assert (iscellstr (c{4})) | |
132 %! assert (isequal (c{3}, [3; -Inf])) | |
133 | |
134 %!test | |
135 %! b = [10:10:100]; | |
136 %! b = [b; 8*b/5]; | |
137 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b); | |
138 %! fmt = "%f miles/hr = %f kilometers/hr"; | |
139 %! c = textscan (str, fmt); | |
140 %! assert (b(1,:)', c{1}) | |
141 %! assert (b(2,:)', c{2}) | |
142 | |
143 |