Mercurial > hg > octave-nkf
annotate scripts/io/textscan.m @ 19241:c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
* textscan.m: more rigid format conversion specifier check & test added
* strread.m: more rigid format conversion specifier check & test added
more to-the-point error messages
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Wed, 13 Aug 2014 21:29:53 +0200 |
parents | d69358d7f9bb |
children | 6ca096827123 |
rev | line source |
---|---|
18717
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
1 ## Copyright (C) 2010-2014 Ben Abbott |
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 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
28 ## The string @var{str} or file associated with @var{fid} is read from and |
14853
72b8b39e12be
doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14800
diff
changeset
|
29 ## parsed according to @var{format}. The function behaves like @code{strread} |
72b8b39e12be
doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14800
diff
changeset
|
30 ## except it can also read from file instead of a string. See the documentation |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
31 ## of @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 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
33 ## In addition to the options supported by @code{strread}, this function |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
34 ## supports a few more: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
35 ## |
11141 | 36 ## @itemize |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
37 ## @item @qcode{"collectoutput"}: |
12914
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 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
42 ## @item @qcode{"endofline"}: |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
43 ## Specify @qcode{"\r"}, @qcode{"\n"} or @qcode{"\r\n"} (for CR, LF, or |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
44 ## CRLF). If no value is given, it will be inferred from the file. If set |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
45 ## to "" (empty string) EOLs are ignored as delimiters and added to |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
46 ## whitespace. |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
47 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
48 ## @item @qcode{"headerlines"}: |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
49 ## 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
|
50 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
51 ## @item @qcode{"returnonerror"}: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
52 ## 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
|
53 ## have been encountered. If set to 0 or false, return an error and no data. |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
54 ## As the string or file is read by columns rather than by rows, and because |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
55 ## textscan is fairly forgiving as regards read errors, setting this option |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
56 ## may have little or no actual effect. |
11141 | 57 ## @end itemize |
58 ## | |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
59 ## When reading from a character string, optional input argument @var{n} |
14621
1804d5422f61
doc: Periodic spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14582
diff
changeset
|
60 ## specifies the number of times @var{format} should be used (i.e., to limit |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
61 ## the amount of data read). |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
62 ## When reading from file, @var{n} specifies the number of data lines to read; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
63 ## in this sense it differs slightly from the format repeat count in strread. |
11141 | 64 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
65 ## The output @var{C} is a cell array whose second dimension is determined |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
66 ## by the number of format specifiers. |
11141 | 67 ## |
68 ## The second output, @var{position}, provides the position, in characters, | |
69 ## from the beginning of the file. | |
70 ## | |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
71 ## If the format string is empty (not: omitted) and the file contains only |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
72 ## numeric data (excluding headerlines), textscan will return data in a number |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
73 ## of columns matching the number of numeric fields on the first data line of |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
74 ## the file. |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
75 ## |
11141 | 76 ## @seealso{dlmread, fscanf, load, strread, textread} |
77 ## @end deftypefn | |
78 | |
16768 | 79 ## Author: Ben Abbott <bpabbott@mac.com> |
80 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
81 function [C, position] = textscan (fid, format = "%f", varargin) |
11141 | 82 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
83 BUFLENGTH = 4096; # Read buffer |
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
84 emptfmt = 0; # Signals deliberately empty format string |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
85 |
11141 | 86 ## Check input |
87 if (nargin < 1) | |
88 print_usage (); | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
89 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
90 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
91 if (isempty (format)) |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
92 emptfmt = 1; |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
93 format = "%f"; |
11141 | 94 endif |
95 | |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
96 if (! ischar (format)) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
97 error ("textscan: FORMAT must be a string"); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
98 endif |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
99 |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
100 ## Determine the number of data fields & initialize output array |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
101 num_fields = numel (regexp (format, '(%(\d*|\d*\.\d*)?[nfduscq]|%\[)', "match")); |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
102 if (! num_fields) |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
103 error ("textscan.m: no valid format conversion specifiers found\n"); |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
104 endif |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
105 C = cell (1, num_fields); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
106 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
107 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
|
108 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
|
109 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
110 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
111 args = varargin; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
112 if (nargin > 2 && isnumeric (args{1})) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
113 nlines = args{1}; |
11141 | 114 else |
115 nlines = Inf; | |
116 endif | |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
117 if (nlines < 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
118 printf ("textscan: N = 0, no data read\n"); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
119 return; endif |
11141 | 120 |
121 if (! any (strcmpi (args, "emptyvalue"))) | |
122 ## Matlab returns NaNs for missing values | |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
123 args(end+1:end+2) = {'emptyvalue', NaN}; |
11141 | 124 endif |
125 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
126 ## 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
|
127 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
128 ipos = find (strcmpi (args, "whitespace")); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
129 if (isempty (ipos)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
130 ## Matlab default whitespace = " \b\t" |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
131 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
|
132 whitespace = " \b\t"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
133 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
134 ## 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
|
135 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
|
136 ## 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
|
137 ## 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
|
138 if (! (isempty (args{ipos+1}) && has_str_fmt)) |
15871
878623ca15e1
textscan.m: fix treatment of whitespace argument (bug #37961)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
139 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
|
140 endif |
18717
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
141 whitespace = args{ipos+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 (! 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
|
145 ## 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
|
146 ## strread() will pick this up further |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
147 args(end+1:end+2) = {'delimiter', ""}; |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
148 delimiter = ""; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
149 else |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
150 delimiter = args{find (strcmpi (args, "delimiter")) + 1}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
151 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
152 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
153 collop = false; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
154 ipos = find (strcmpi (args, "collectoutput")); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
155 if (! isempty (ipos)) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
156 ## 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
|
157 if (isscalar (args{ipos+1}) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
158 && (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
|
159 collop = args{ipos+1}; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
160 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
161 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
|
162 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
163 ## 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
|
164 args(ipos:ipos+1) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
165 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
166 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
167 if (any (strcmpi (args, "returnonerror"))) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
168 ## 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
|
169 ## 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
|
170 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
|
171 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
172 ## Set default value (=true) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
173 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
|
174 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
175 |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
176 ## Check if a headerlines argument is specified |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
177 headerlines = find (strcmpi (args, "headerlines"), 1); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
178 if (! isempty (headerlines)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
179 ## Yep. But it is stray when reading from strings... |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
180 if (ischar (fid)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
181 warning ("textscan: 'headerlines' ignored when reading from strings"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
182 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
183 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
184 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
185 if (ischar (fid)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
186 ## Read from a text string |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
187 if (nargout == 2) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
188 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
|
189 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
190 str = fid; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
191 else |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
192 st_pos = ftell (fid); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
193 ## Skip header lines if requested |
16060
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
194 if (! isempty (headerlines)) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
195 ## Beware of missing or wrong headerline value |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
196 if (headerlines == numel (args) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
197 || ! isnumeric (args{headerlines + 1})) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
198 error ("Missing or illegal value for 'headerlines'" ); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
199 endif |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
200 ## Avoid conveying floats to fskipl |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
201 args{headerlines + 1} = round (args{headerlines + 1}); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
202 if (args{headerlines + 1} > 0) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
203 ## Beware of zero valued headerline, fskipl would skip to EOF |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
204 fskipl (fid, args{headerlines + 1}); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
205 args(headerlines:headerlines+1) = []; |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
206 st_pos = ftell (fid); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
207 elseif (args{headerlines + 1} < 0) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
208 warning ("textscan.m: negative headerline value ignored"); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
209 endif |
16305
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
210 endif |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
211 ## Read a first file chunk. Rest follows after endofline processing |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
212 [str, count] = fscanf (fid, "%c", BUFLENGTH); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
213 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
214 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
215 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
216 ## Check for empty result |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
217 if (isempty (str)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
218 warning ("textscan: no data read"); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
219 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
220 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
221 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
222 ## 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
|
223 endofline = find (strcmpi (args, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
224 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
225 if (ischar (args{endofline + 1})) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
230 else |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
231 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
|
232 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
233 else |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
234 if (! ischar (fid)) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
235 ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
236 eol_srch_len = min (length (str), BUFLENGTH); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
237 ## First try DOS (CRLF) |
16311
9c4ac8f25a8c
textscan.m, textread.m: fix wrong code assessing EOL char(s), remove duplicate code
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16305
diff
changeset
|
238 if (! isempty (strfind (str(1 : eol_srch_len), "\r\n"))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
239 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
240 ## Perhaps old Macintosh? (CR) |
16311
9c4ac8f25a8c
textscan.m, textread.m: fix wrong code assessing EOL char(s), remove duplicate code
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16305
diff
changeset
|
241 elseif (! isempty (strfind (str(1 : eol_srch_len), "\r"))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
242 eol_char = "\r"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
243 ## Otherwise, use plain UNIX (LF) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
244 else |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
245 eol_char = "\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
246 endif |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
247 else |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
248 eol_char = "\n"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
249 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
250 ## Set up the default endofline param value |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
251 args(end+1:end+2) = {"endofline", eol_char}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
252 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
253 |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
254 if (!ischar (fid)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
255 ## Now that we know what EOL looks like, we can process format_repeat_count. |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
256 ## FIXME The below isn't ML-compatible: counts lines, not format string uses |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
257 if (isfinite (nlines) && (nlines >= 0)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
258 l_eol_char = length (eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
259 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
260 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
261 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
262 ## Avoid slow repeated str concatenation, first seek requested end of data |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
263 while (n_eoi < nlines && count == BUFLENGTH) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
264 [nstr, count] = fscanf (fid, "%c", BUFLENGTH); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
265 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
266 ## Watch out for multichar EOL being missed across buffer boundaries |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
267 if (l_eol_char > 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
268 str = [str(end - length (eol_char) + 2 : end) nstr]; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
269 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
270 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
271 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
272 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
273 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
274 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
275 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
276 endwhile |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
277 ## OK, found EOL delimiting last requested line. Compute ptr (incl. EOL) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
278 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
279 printf ("textscan: format repeat count specified but no endofline found\n"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
280 data_size = nblks * BUFLENGTH + count; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
281 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
282 ## Compute data size to read incl complete EOL |
17245
7babcdb9bc13
Use ... instead of \ for line continuation marker.
Stefan Mahr <dac922@gmx.de>
parents:
16768
diff
changeset
|
283 data_size = (nblks * BUFLENGTH) + eoi(end + min (nlines, n_eoi) - n_eoi) ... |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
284 + l_eol_char - 1; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
285 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
286 fseek (fid, st_pos, "bof"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
287 str = fscanf (fid, "%c", data_size); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
288 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
289 fseek (fid, st_pos, "bof"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
290 str = fread (fid, "char=>char").'; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
291 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
292 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
293 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
294 ## Strip trailing EOL to avoid returning stray missing values (f. strread). |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
295 ## However, in case of CollectOutput request, presence of EOL is required; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
296 ## also in case of deliberately entered empty format string |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
297 eol_at_end = strcmp (str(end-length (eol_char) + 1 : end), eol_char); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
298 if (collop || emptfmt) |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
299 if (! eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
300 str(end+1 : end+length (eol_char)) = eol_char; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
301 endif |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
302 elseif (eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
303 str(end-length (eol_char) + 1 : end) = ""; |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
304 ## A corner case: str may now be empty.... |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
305 if (isempty (str)); return; endif |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
306 endif |
11141 | 307 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
308 ## 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
|
309 C = cell (1, num_fields); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
310 [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
|
311 |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
312 ## I.c.o. empty format, match nr. of cols to nr. of fields on first read line |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
313 if (emptfmt) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
314 ## Find end of first line |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
315 eoi = index (str, eol_char); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
316 if (eoi) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
317 ## str contains an EOL, proceed with assessing nr. of columns |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
318 ncols = countcols (C, str(1 : eoi-1), delimiter, whitespace); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
319 ## See if lowermost data row must be completed |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
320 pad = mod (numel (C{1}), ncols); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
321 if (pad) |
18989
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
322 ## Pad output with emptyvalues (rest has been done by stread.m) |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
323 emptv = find (strcmpi (args, "emptyvalue")); |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
324 if (isempty (emptv)) |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
325 ## By default textscan returns NaNs for empty fields |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
326 C(1) = [C{1}; NaN(ncols - pad, 1)]; |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
327 else |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
328 ## Otherwise return supplied emptyvalue. Pick last occurrence |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
329 C(1) = [C{1}; repmat(args{emptv(end)+1}, ncols - pad, 1)]; |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
330 endif |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
331 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
332 ## Compute nr. of rows |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
333 nrows = floor (numel (C{1}) / ncols); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
334 ## Reshape C; watch out, transpose needed |
19199
d69358d7f9bb
textscan.m: Avoid conjugate transpose for complex data and empty format string (bug #42528)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18989
diff
changeset
|
335 C(1) = reshape (C{1}, ncols, numel (C{1}) / ncols).'; |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
336 ## Distribute columns over C and wipe cols 2:end of C{1} |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
337 for ii=2:ncols |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
338 C(ii) = C{1}(:, ii); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
339 endfor |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
340 C{1} = C{1}(:, 1); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
341 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
342 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
343 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
344 ## 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
|
345 if (collop) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
346 C = colloutp (C); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
347 endif |
11141 | 348 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
349 if (nargout == 2) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
350 ## Remember file position (persistent var) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
351 position = ftell (fid); |
11141 | 352 endif |
353 | |
354 endfunction | |
355 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
356 |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
357 ## Assess nr of data fields on first line of data |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
358 function ncols = countcols (C, str, dlm, wsp) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
359 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
360 if (isempty (dlm)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
361 ## Field separator = whitespace. Fold multiple whitespace into one |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
362 str = regexprep (str, sprintf ("[%s]", wsp), " "); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
363 str = strtrim (str); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
364 ncols = numel (strfind (str, " ")) + 1; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
365 else |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
366 ncols = numel (regexp (str, sprintf ("[%s]", dlm))) + 1; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
367 endif |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
368 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
369 endfunction |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
370 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
371 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
372 ## 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
|
373 function C = colloutp (C) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
374 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
375 ## 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
|
376 ii = numel (C); |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
377 while (ii > 1) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
378 clss1 = class (C{ii}); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
379 jj = ii; |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
380 while (jj > 1 && strcmp (clss1, class (C{jj - 1}))) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
381 ## 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
|
382 --jj; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
383 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
384 if (jj < ii) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
385 ## Concatenate columns into current column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
386 C{jj} = [C{jj : ii}]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
387 ## 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
|
388 C(jj+1 : ii) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
389 ii = jj - 1; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
390 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
391 ## 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
|
392 --ii; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
393 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
394 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
395 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
396 endfunction |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
397 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
398 |
11141 | 399 %!test |
400 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12"; | |
401 %! fmtstr = "%f %d %f %s"; | |
402 %! 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
|
403 %! assert (isequal (c{1}, [1;5])); |
11141 | 404 %! 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
|
405 %! assert (iscellstr (c{4})); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
406 %! assert (isequal (c{3}, [3; -Inf])); |
11141 | 407 |
408 %!test | |
409 %! b = [10:10:100]; | |
410 %! b = [b; 8*b/5]; | |
411 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b); | |
412 %! fmt = "%f miles/hr = %f kilometers/hr"; | |
413 %! c = textscan (str, fmt); | |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
414 %! 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
|
415 %! 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
|
416 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
417 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
418 %! str = "13, 72, NA, str1, 25\r\n// Middle line\r\n36, na, 05, str3, 6"; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
419 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",","treatAsEmpty", {"NA", "na"},"commentStyle", "//"); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
420 %! assert (a{1}, int32 ([13; 36])); |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
421 %! assert (a{2}, [72; NaN]); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
422 %! assert (a{3}, [NaN; 5]); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
423 %! assert (a{4}, {"str1"; "str3"}); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
424 %! assert (a{5}, [25; 6]); |
11141 | 425 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
426 %!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
427 %! 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
|
428 %! 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
|
429 %! 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
|
430 %! 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
|
431 %! fmt = "Km:%d = hhh%1sjjj miles%dhour"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
432 %! a = textscan (str, fmt, "delimiter", " "); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
433 %! 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
|
434 %! assert (a{2}', {'B' 'J' 'R' 'Z'}); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
435 %! assert (a{3}', int32 ([16 241 3 0])); |
11141 | 436 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
437 %% Test with default endofline parameter |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
438 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
439 %! c = textscan ("L1\nL2", "%s"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
440 %! assert (c{:}, {"L1"; "L2"}); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
441 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
442 %% Test with endofline parameter set to "" (empty) - newline should be in word |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
443 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
444 %! c = textscan ("L1\nL2", "%s", "endofline", ""); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
445 %! assert (int8 (c{:}{:}), int8 ([ 76, 49, 10, 76, 50 ])); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
446 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
447 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
448 %! ## No delimiters at all besides EOL. Skip fields, even empty fields |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
449 %! str = "Text1Text2Text\nTextText4Text\nText57Text"; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
450 %! c = textscan (str, "Text%*dText%dText"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
451 %! 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
|
452 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
453 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
454 %% CollectOutput test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
455 %! b = [10:10:100]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
456 %! 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
|
457 %! 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
|
458 %! fmt = "%f miles%s %s %f (%f) kilometers %*s"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
459 %! c = textscan (str, fmt, "collectoutput", 1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
460 %! assert (size (c{3}), [10, 2]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
461 %! assert (size (c{2}), [10, 2]); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
462 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
463 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
464 %% CollectOutput test with uneven column length files |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
465 %! b = [10:10:100]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
466 %! b = [b; 8*b/5; 8*b*1000/5]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
467 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
468 %! str = [str "110 miles/hr"]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
469 %! fmt = "%f miles%s %s %f (%f) kilometers %*s"; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
470 %! c = textscan (str, fmt, "collectoutput", 1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
471 %! assert (size (c{1}), [11, 1]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
472 %! assert (size (c{3}), [11, 2]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
473 %! assert (size (c{2}), [11, 2]); |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
474 %! assert (c{3}(end), NaN); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
475 %! assert (c{2}{11, 1}, "/hr"); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
476 %! assert (isempty (c{2}{11, 2}), true); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
477 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
478 %% Test input validation |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
479 %!error textscan () |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
480 %!error textscan (single (4)) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
481 %!error textscan ({4}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
482 %!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
|
483 %!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
|
484 %!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
|
485 |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
486 %! Test incomplete first data line |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
487 %! R = textscan (['Empty1' char(10)], 'Empty%d %f'); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
488 %! assert (R{1}, int32 (1)); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
489 %! assert (isempty (R{2}), true); |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
490 |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
491 %% bug #37023 (actually a strread test) |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
492 %!test |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
493 %! data = textscan(" 1. 1 \n 2 3\n", '%f %f'); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
494 %! assert (data{1}, [1; 2], 1e-15); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
495 %! assert (data{2}, [1; 3], 1e-15); |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
496 |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
497 %% Whitespace test (bug #37333) using delimiter ";" |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
498 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
499 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
500 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
501 %! tc{1, end+1} = "C:/code/meas;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
502 %! tc{1, end+1} = " C:/code/sim;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
503 %! tc{1, end+1} = "C:/code/utils;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
504 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
505 %! c = textscan (string, "%s", "delimiter", ";"); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
506 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
507 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
508 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
509 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
510 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
511 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
512 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
513 |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
514 %% Whitespace test (bug #37333), adding multipleDelimsAsOne true arg |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
515 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
516 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
517 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
518 %! tc{1, end+1} = " C:/code/meas;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
519 %! tc{1, end+1} = "C:/code/sim;;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
520 %! tc{1, end+1} = "C:/code/utils;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
521 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
522 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
523 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
524 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
525 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
526 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
527 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
528 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
529 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
530 |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
531 %% Whitespace test (bug #37333), adding multipleDelimsAsOne false arg |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
532 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
533 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
534 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
535 %! tc{1, end+1} = " C:/code/meas;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
536 %! tc{1, end+1} = "C:/code/sim;;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
537 %! tc{1, end+1} = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
538 %! tc{1, end+1} = "C:/code/utils;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
539 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
540 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
541 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
542 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
543 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
544 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
545 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
546 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
547 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
548 |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
549 %% Whitespace test (bug #37333) whitespace "" arg |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
550 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
551 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
552 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
553 %! tc{1, end+1} = " C:/code/meas;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
554 %! tc{1, end+1} = "C:/code/sim;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
555 %! tc{1, end+1} = "C:/code/utils;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
556 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
557 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", ""); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
558 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
559 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
560 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
561 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
562 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
563 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
564 |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
565 %% Whitespace test (bug #37333), whitespace " " arg |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
566 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
567 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
568 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
569 %! tc{1, end+1} = " C:/code/meas;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
570 %! tc{1, end+1} = "C:/code/sim;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
571 %! tc{1, end+1} = "C:/code/utils;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
572 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
573 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " "); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
574 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
575 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
576 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
577 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
578 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
579 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
580 %! end |
16060
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
581 |
16305
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
582 %% Test reading from a real file |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
583 %!test |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
584 %! f = tmpnam (); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
585 %! fid = fopen (f, "w+"); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
586 %! d = rand (1, 4); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
587 %! fprintf (fid, " %f %f %f %f ", d); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
588 %! fseek (fid, 0, "bof"); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
589 %! A = textscan (fid, "%f %f"); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
590 %! fclose (fid); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
591 %! unlink (f); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
592 %! assert (A{1}, [d(1); d(3)], 1e-6); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
593 %! assert (A{2}, [d(2); d(4)], 1e-6); |
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
594 |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
595 %% Tests reading with empty format, should return proper nr of columns |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
596 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
597 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
598 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
599 %! fprintf (fid, " 1 2 3 4\n5 6 7 8"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
600 %! fseek (fid, 0, "bof"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
601 %! A = textscan (fid, ""); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
602 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
603 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
604 %! assert (A{1}, [1 ; 5], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
605 %! assert (A{2}, [2 ; 6], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
606 %! assert (A{3}, [3 ; 7], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
607 %! assert (A{4}, [4 ; 8], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
608 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
609 %% Tests reading with empty format; empty fields & incomplete lower row |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
610 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
611 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
612 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
613 %! fprintf (fid, " ,2,,4\n5,6"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
614 %! fseek (fid, 0, "bof"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
615 %! A = textscan (fid, "", "delimiter", ",", "EmptyValue", 999, "CollectOutput" , 1); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
616 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
617 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
618 %! assert (A{1}, [999, 2, 999, 4; 5, 6, 999, 999], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
619 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
620 %% Error message tests |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
621 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
622 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
623 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
624 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
625 %! msg1 = "Missing or illegal value for 'headerlines'"; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
626 %! try |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
627 %! A = textscan (fid, "", "headerlines"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
628 %! end_try_catch; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
629 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
630 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
631 %! assert (msg1, lasterr); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
632 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
633 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
634 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
635 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
636 %! msg1 = "Missing or illegal value for 'headerlines'"; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
637 %! try |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
638 %! A = textscan (fid, "", "headerlines", "hh"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
639 %! end_try_catch; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
640 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
641 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
642 %! assert (msg1, lasterr); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
643 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
644 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
645 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
646 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
647 %! fprintf (fid,"some_string"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
648 %! fseek (fid, 0, "bof"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
649 %! msg1 = "textscan: illegal EndOfLine character value specified"; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
650 %! try |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
651 %! A = textscan (fid, "%f", "EndOfLine", "\n\r"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
652 %! end_try_catch; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
653 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
654 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
655 %! assert (msg1, lasterr); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
656 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
657 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
658 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
659 %! fid = fopen (f, "w+"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
660 %! fprintf (fid,"some_string"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
661 %! fseek (fid, 0, "bof"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
662 %! msg1 = "textscan: character value required for EndOfLine"; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
663 %! try |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
664 %! A = textscan (fid, "%f", "EndOfLine", 33); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
665 %! end_try_catch; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
666 %! fclose (fid); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
667 %! unlink (f); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
668 %! assert (msg1, lasterr); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17336
diff
changeset
|
669 |
18717
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
670 %% Bug #41824 |
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
671 %!test |
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
672 %! assert (textscan ("123", "", "whitespace", " "){:}, 123); |
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
673 |
18989
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
674 %% Bug #42343-1, just test supplied emptyvalue (actually done by strread.m) |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
675 %!test |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
676 %! assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf), {Inf, NaN}); |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
677 |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
678 %% Bug #42343-2, test padding with supplied emptyvalue (done by textscan.m) |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
679 %!test |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
680 %! a = textscan (",1,,4\nInf, ,NaN", "", "delimiter", ",", "emptyvalue", -10); |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
681 %! assert (cell2mat (a), [-10, 1, -10, 4; Inf, -10, NaN, -10]); |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
682 |
19199
d69358d7f9bb
textscan.m: Avoid conjugate transpose for complex data and empty format string (bug #42528)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18989
diff
changeset
|
683 %% Bug #42528 |
d69358d7f9bb
textscan.m: Avoid conjugate transpose for complex data and empty format string (bug #42528)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18989
diff
changeset
|
684 %!test |
d69358d7f9bb
textscan.m: Avoid conjugate transpose for complex data and empty format string (bug #42528)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18989
diff
changeset
|
685 %! assert (textscan ("1i", ""){1}, 0+1i); |
d69358d7f9bb
textscan.m: Avoid conjugate transpose for complex data and empty format string (bug #42528)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18989
diff
changeset
|
686 %! assert (cell2mat (textscan ("3, 2-4i, NaN\n -i, 1, 23.4+2.2i", "")), [3+0i, 2-4i, NaN+0i; 0-i, 1+0i, 23.4+2.2i]); |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
687 |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
688 %% Illegal format specifiers |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
689 %!test |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
690 %!error <no valid format conversion specifiers> textscan ("1.0", "%z"); |