Mercurial > hg > octave-nkf
annotate scripts/io/textscan.m @ 14214:2fe0f5fa8cc3
Replace to-be-deprecated findstr occurrences with strfind.
* help.m, textread.m, textscan.m, pkg.m, legend.m, __ezplot__.m,
__go_draw_axes__.m, rundemos.m: Replace to-be-deprecated findstr occurrences
with strfind.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 18 Jan 2012 21:15:42 -0800 |
parents | a022c04f68cc |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
1 ## Copyright (C) 2010-2012 Ben Abbott <bpabbott@mac.com> |
11141 | 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 -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
20 ## @deftypefn {Function File} {@var{C} =} textscan (@var{fid}, @var{format}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{n}) |
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{}) |
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{n}, @var{param}, @var{value}, @dots{}) |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{str}, @dots{}) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{C}, @var{position}] =} textscan (@var{fid}, @dots{}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
26 ## Read data from a text file or string. |
11141 | 27 ## |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
28 ## The file associated with @var{fid} is read and parsed according to |
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## @var{format}. The function behaves like @code{strread} except it works by |
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
30 ## parsing a file instead of a string. See the documentation of |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
31 ## @code{strread} for details. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
32 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
33 ## In addition to the options supported by |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
34 ## @code{strread}, this function supports a few more: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
35 ## |
11141 | 36 ## @itemize |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
37 ## @item "collectoutput": |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
38 ## A value of 1 or true instructs textscan to concatenate consecutive columns |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
39 ## of the same class in the output cell array. A value of 0 or false (default) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
40 ## leaves output in distinct columns. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
41 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
42 ## @item "endofline": |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
43 ## Specify "\r", "\n" or "\r\n" (for CR, LF, or CRLF). If no value is given, |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
44 ## it will be inferred from the file. If set to "" (empty string) EOLs are |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
45 ## ignored as delimiters and added to whitespace. |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
46 ## |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
47 ## @item "headerlines": |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
48 ## The first @var{value} number of lines of @var{fid} are skipped. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
49 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
50 ## @item "returnonerror": |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
51 ## If set to numerical 1 or true (default), return normally when read errors |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
52 ## have been encountered. If set to 0 or false, return an error and no data. |
11141 | 53 ## @end itemize |
54 ## | |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
55 ## The optional input @var{n} specifes the number of times to use |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
56 ## @var{format} when parsing, i.e., the format repeat count. |
11141 | 57 ## |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
58 ## The output @var{C} is a cell array whose length is given by the number |
11141 | 59 ## of format specifiers. |
60 ## | |
61 ## The second output, @var{position}, provides the position, in characters, | |
62 ## from the beginning of the file. | |
63 ## | |
64 ## @seealso{dlmread, fscanf, load, strread, textread} | |
65 ## @end deftypefn | |
66 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
67 function [C, position] = textscan (fid, format = "%f", varargin) |
11141 | 68 |
69 ## Check input | |
70 if (nargin < 1) | |
71 print_usage (); | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
72 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
73 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
74 if (isempty (format)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
75 format = "%f"; |
11141 | 76 endif |
77 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
78 if (! (isa (fid, "double") && fid > 0) && ! ischar (fid)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
79 error ("textscan: first argument must be a file id or character string"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
80 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
81 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
82 if (! ischar (format)) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
83 error ("textscan: FORMAT must be a string"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
84 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
85 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
86 args = varargin; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
87 if (nargin > 2 && isnumeric (args{1})) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
88 nlines = args{1}; |
11141 | 89 else |
90 nlines = Inf; | |
91 endif | |
92 | |
93 if (! any (strcmpi (args, "emptyvalue"))) | |
94 ## Matlab returns NaNs for missing values | |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
95 args(end+1:end+2) = {'emptyvalue', NaN}; |
11141 | 96 endif |
97 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
98 ## Check default parameter values that differ for strread & textread |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
99 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
100 ipos = find (strcmpi (args, "whitespace")); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
101 if (isempty (ipos)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
102 ## Matlab default whitespace = " \b\t" |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
103 args(end+1:end+2) = {'whitespace', " \b\t"}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
104 whitespace = " \b\t"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
105 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
106 ## Check if there's at least one string format specifier |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 has_str_fmt = regexp (format, '%[*]?\d*s', "once"); |
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 ## If there is a string format AND whitespace value = empty, |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
109 ## don't add a space (char(32)) to whitespace |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 if (! (isempty (args{ipos+1}) && has_str_fmt)) |
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 args{ipos+1} = unique ([" ", args{ipos+1}]); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
112 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
113 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
114 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
115 if (! any (strcmpi (args, "delimiter"))) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
116 ## Matlab says default delimiter = whitespace. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
117 ## strread() will pick this up further |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
118 args(end+1:end+2) = {'delimiter', ""}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
119 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
120 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
121 collop = false; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
122 ipos = find (strcmpi (args, "collectoutput")); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
123 if (! isempty (ipos)) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
124 ## Search & concatenate consecutive columns of same class requested |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
125 if (isscalar (args{ipos+1}) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
126 && (islogical (args{ipos+1}) || isnumeric (args{ipos+1}))) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
127 collop = args{ipos+1}; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
128 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
129 warning ("textscan: illegal value for CollectOutput parameter - ignored"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
130 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
131 ## Remove argument before call to strread() below |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
132 args(ipos:ipos+1) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
133 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
134 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
135 if (any (strcmpi (args, "returnonerror"))) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
136 ## Because of the way strread() reads data (columnwise) this parameter |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
137 ## can't be neatly implemented. strread() will pick it up anyway |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
138 warning ('textscan: ReturnOnError is not fully implemented'); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
139 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
140 ## Set default value (=true) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
141 args(end+1:end+2) = {"returnonerror", 1}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
142 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
143 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
144 if (ischar (fid)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
145 ## Read from a text string |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
146 if (nargout == 2) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
147 error ("textscan: cannot provide position information for character input"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
148 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
149 str = fid; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
150 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
151 ## Skip header lines if requested |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
152 headerlines = find (strcmpi (args, "headerlines"), 1); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
153 ## Beware of zero valued headerline, fskipl would skip to EOF |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
154 if (! isempty (headerlines) && (args{headerlines + 1} > 0)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
155 fskipl (fid, varargin{headerlines + 1}); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
156 args(headerlines:headerlines+1) = []; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
157 endif |
12880
ff264eae88cf
textread.m, textscan.m: Read entire file when format repeat count is -1
Rik <octave@nomad.inbox5.com>
parents:
12877
diff
changeset
|
158 if (isfinite (nlines) && (nlines >= 0)) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
159 str = tmp_str = ""; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
160 n = 0; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
161 ## FIXME: Can this be done without slow loop? |
12916
5d0478a75618
textscan: Fix multiple accesses, bug #33876.
Ben Abbott <bpabbott@mac.com>
parents:
12914
diff
changeset
|
162 while (ischar (tmp_str) && n++ < nlines) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
163 tmp_str = fgets (fid); |
12916
5d0478a75618
textscan: Fix multiple accesses, bug #33876.
Ben Abbott <bpabbott@mac.com>
parents:
12914
diff
changeset
|
164 if (ischar (tmp_str)) |
5d0478a75618
textscan: Fix multiple accesses, bug #33876.
Ben Abbott <bpabbott@mac.com>
parents:
12914
diff
changeset
|
165 str = strcat (str, tmp_str); |
5d0478a75618
textscan: Fix multiple accesses, bug #33876.
Ben Abbott <bpabbott@mac.com>
parents:
12914
diff
changeset
|
166 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
167 endwhile |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
168 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
169 str = fread (fid, "char=>char").'; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
170 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
171 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
172 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
173 ## Check for empty result |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
174 if (isempty (str)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
175 warning ("textscan: no data read"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
176 C = []; |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
177 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
178 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
179 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
180 ## Check value of 'endofline'. String or file doesn't seem to matter |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
181 endofline = find (strcmpi (args, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
182 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
183 if (ischar (args{endofline + 1})) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
184 eol_char = args{endofline + 1}; |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
185 if (! any (strcmp (eol_char, {"", "\n", "\r", "\r\n"}))) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
186 error ("textscan: illegal EndOfLine character value specified"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
187 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
188 else |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
189 error ("textscan: character value required for EndOfLine"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
190 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
191 else |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
192 ## Determine EOL from file. Search for EOL candidates in first 3000 chars |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
193 eol_srch_len = min (length (str), 3000); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
194 ## First try DOS (CRLF) |
14214
2fe0f5fa8cc3
Replace to-be-deprecated findstr occurrences with strfind.
Rik <octave@nomad.inbox5.com>
parents:
14213
diff
changeset
|
195 if (! isempty (strfind ("\r\n", str(1 : eol_srch_len)))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
196 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
197 ## Perhaps old Macintosh? (CR) |
14214
2fe0f5fa8cc3
Replace to-be-deprecated findstr occurrences with strfind.
Rik <octave@nomad.inbox5.com>
parents:
14213
diff
changeset
|
198 elseif (! isempty (strfind ("\r", str(1 : eol_srch_len)))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
199 eol_char = "\r"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
200 ## Otherwise, use plain UNIX (LF) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
201 else |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
202 eol_char = "\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
203 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
204 ## Set up the default endofline param value |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
205 args(end+1:end+2) = {'endofline', eol_char}; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
206 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
207 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
208 ## Determine the number of data fields |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
209 num_fields = numel (strfind (format, "%")) - numel (strfind (format, "%*")); |
11141 | 210 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
211 ## Strip trailing EOL to avoid returning stray missing values (f. strread) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
212 if (strcmp (str(end-length (eol_char) + 1 : end), eol_char)); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
213 str(end-length (eol_char) + 1 : end) = ""; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
214 endif |
11141 | 215 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
216 ## Call strread to make it do the real work |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
217 C = cell (1, num_fields); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
218 [C{:}] = strread (str, format, args{:}); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
219 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
220 ## If requested, collect output columns of same class |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
221 if (collop) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
222 C = colloutp (C); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
223 endif |
11141 | 224 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
225 if (nargout == 2) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
226 position = ftell (fid); |
11141 | 227 endif |
228 | |
229 endfunction | |
230 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
231 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
232 ## Collect consecutive columns of same class into one cell column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
233 function C = colloutp (C) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
234 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
235 ## Start at rightmost column and work backwards to avoid ptr mixup |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
236 ii = numel (C); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
237 while ii > 1 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
238 clss1 = class (C{ii}); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
239 jj = ii; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
240 while (jj > 1 && strcmp (clss1, class (C{jj - 1}))) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
241 ## Column to the left is still same class; check next column to the left |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
242 --jj; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
243 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
244 if (jj < ii) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
245 ## Concatenate columns into current column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
246 C{jj} = [C{jj : ii}]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
247 ## Wipe concatenated columns to the right, resume search to the left |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
248 C(jj+1 : ii) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
249 ii = jj - 1; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
250 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
251 ## No similar class in column to the left, search from there |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
252 --ii; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
253 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
254 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
255 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
256 endfunction |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
257 |
11141 | 258 %!test |
259 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12"; | |
260 %! fmtstr = "%f %d %f %s"; | |
261 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf); | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
262 %! assert (isequal (c{1}, [1;5])); |
11141 | 263 %! assert (length (c{1}), 2); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
264 %! assert (iscellstr (c{4})); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
265 %! assert (isequal (c{3}, [3; -Inf])); |
11141 | 266 |
267 %!test | |
268 %! b = [10:10:100]; | |
269 %! b = [b; 8*b/5]; | |
270 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b); | |
271 %! fmt = "%f miles/hr = %f kilometers/hr"; | |
272 %! c = textscan (str, fmt); | |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
273 %! assert (b(1,:)', c{1}, 1e-5); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
274 %! assert (b(2,:)', c{2}, 1e-5); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
275 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
276 #%!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
277 #%! str = "13, 72, NA, str1, 25\r\n// Middle line\r\n36, na, 05, str3, 6"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
278 #%! a = textscan(str, '%d %n %f %s %n', 'delimiter', ',','treatAsEmpty', {'NA', 'na'},'commentStyle', '//'); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
279 #%! assert (a{1}, int32([13; 36])); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
280 #%! assert (a{2}, [72; NaN]); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
281 #%! assert (a{3}, [NaN; 5]); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
282 #%! assert (a{4}, {"str1"; "str3"}); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
283 #%! assert (a{5}, [25; 6]); |
11141 | 284 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
285 %!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
286 %! str = "Km:10 = hhhBjjj miles16hour\r\n"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
287 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
288 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
289 %! str = [str "Km:25 = hhhZ\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
290 %! fmt = "Km:%d = hhh%1sjjj miles%dhour"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
291 %! a = textscan (str, fmt, 'delimiter', ' '); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
292 %! assert (a{1}', int32([10 15 2 25])); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
293 %! assert (a{2}', {'B' 'J' 'R' 'Z'}); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
294 %! assert (a{3}', int32([16 241 3 0])); |
11141 | 295 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
296 %% Test with default endofline parameter |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
297 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
298 %! c = textscan ("L1\nL2", "%s"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
299 %! assert (c{:}, {"L1"; "L2"}); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
300 |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
301 %% Test with endofline parameter set to '' (empty) - newline should be in word |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
302 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
303 %! c = textscan ("L1\nL2", "%s", 'endofline', ''); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
304 %! assert (int8(c{:}{:}), int8([ 76, 49, 10, 76, 50 ])); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
305 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
306 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
307 %! # No delimiters at all besides EOL. Skip fields, even empty fields |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
308 %! str = "Text1Text2Text\nTextText4Text\nText57Text"; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
309 %! c = textscan (str, "Text%*dText%dText"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
310 %! assert (c{1}, int32 ([2; 4; 0])); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
311 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
312 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
313 %% CollectOutput test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
314 %! b = [10:10:100]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
315 %! b = [b; 8*b/5; 8*b*1000/5]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
316 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
317 %! fmt = "%f miles%s %s %f (%f) kilometers %*s"; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
318 %! c = textscan (str, fmt, 'collectoutput', 1); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
319 %! assert (size(c{3}), [10, 2]); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
320 %! assert (size(c{2}), [10, 2]); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
321 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
322 %% Test input validation |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
323 %!error textscan () |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
324 %!error textscan (single (4)) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
325 %!error textscan ({4}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
326 %!error <must be a string> textscan ("Hello World", 2) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
327 %!error <cannot provide position information> [C, pos] = textscan ("Hello World") |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
328 %!error <character value required> textscan ("Hello World", '%s', 'EndOfLine', 3) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
329 |