Mercurial > hg > octave-nkf
annotate scripts/io/textscan.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 66adbb6e88ad |
children |
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"}: |
20311
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
43 ## Specify @qcode{"@xbackslashchar{}r"}, @qcode{"@xbackslashchar{}n"} or |
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
44 ## @qcode{"@xbackslashchar{}r@xbackslashchar{}n"} (for CR, LF, or CRLF). If no |
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
45 ## value is given, it will be inferred from the file. If set to "" (empty |
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
46 ## string) EOLs are ignored as delimiters and added to 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 st_pos = ftell (fid); |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
206 elseif (args{headerlines + 1} < 0) |
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
207 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
|
208 endif |
20775
66adbb6e88ad
textread.m, textscan.m: always remove headerlines args before invoking strread (bug #46080)
Ethan Biery <ebiery@lutron.com>
parents:
20431
diff
changeset
|
209 args(headerlines:headerlines+1) = []; |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18717
diff
changeset
|
210 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
|
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)) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
235 ## Determine EOL from file. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
236 ## Search for EOL candidates in the first BUFLENGTH chars |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
237 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
|
238 ## 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
|
239 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
|
240 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
241 ## 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
|
242 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
|
243 eol_char = "\r"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
244 ## Otherwise, use plain UNIX (LF) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
245 else |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
246 eol_char = "\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
247 endif |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
248 else |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
249 eol_char = "\n"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
250 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
251 ## 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
|
252 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
|
253 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
254 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
255 if (! ischar (fid)) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
256 ## 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
|
257 ## 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
|
258 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
|
259 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
|
260 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
|
261 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
262 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
263 ## 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
|
264 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
|
265 [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
|
266 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
267 ## 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
|
268 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
|
269 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
|
270 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
271 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
272 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
273 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
|
274 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
275 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
276 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
277 endwhile |
20430
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20311
diff
changeset
|
278 ## Handle case of missing trailing EOL |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20311
diff
changeset
|
279 if (! strcmp (str(end - length (eol_char) + 1 : end), eol_char)) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20311
diff
changeset
|
280 eoi = [ eoi (length (str)) ]; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20311
diff
changeset
|
281 ++n_eoi; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20311
diff
changeset
|
282 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
283 ## 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
|
284 if (isempty (eoi)) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
285 disp ("textscan: format repeat count specified but no endofline found"); |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
286 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
|
287 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
288 ## Compute data size to read incl complete EOL |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
289 data_size = (nblks * BUFLENGTH) ... |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
290 + 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
|
291 + l_eol_char - 1; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
292 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
293 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
|
294 str = fscanf (fid, "%c", data_size); |
20431
1b4bef838aaf
textscan.m: adapt format repeat counter when reading from file before invoking strread.m (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20430
diff
changeset
|
295 args{1} = Inf; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
296 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
297 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
|
298 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
|
299 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
300 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
301 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
302 ## 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
|
303 ## 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
|
304 ## 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
|
305 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
|
306 if (collop || emptfmt) |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
307 if (! eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
308 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
|
309 endif |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
310 elseif (eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
311 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
|
312 ## 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
|
313 if (isempty (str)); return; endif |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
314 endif |
11141 | 315 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
316 ## 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
|
317 C = cell (1, num_fields); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
318 [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
|
319 |
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
|
320 ## 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
|
321 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
|
322 ## 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
|
323 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
|
324 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
|
325 ## 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
|
326 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
|
327 ## 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
|
328 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
|
329 if (pad) |
18989
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
330 ## 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
|
331 emptv = find (strcmpi (args, "emptyvalue")); |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
332 if (isempty (emptv)) |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
333 ## 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
|
334 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
|
335 else |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
336 ## 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
|
337 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
|
338 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
|
339 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
|
340 ## 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
|
341 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
|
342 ## 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
|
343 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
|
344 ## 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
|
345 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
|
346 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
|
347 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
|
348 C{1} = C{1}(:, 1); |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18717
diff
changeset
|
349 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
|
350 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
|
351 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
352 ## 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
|
353 if (collop) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
354 C = colloutp (C); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
355 endif |
11141 | 356 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
357 if (nargout == 2) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
358 ## Remember file position (persistent var) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
359 position = ftell (fid); |
11141 | 360 endif |
361 | |
362 endfunction | |
363 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
364 |
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
|
365 ## 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
|
366 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
|
367 |
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 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
|
369 ## 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 |
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
|
377 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
|
378 |
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
|
379 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
380 ## 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
|
381 function C = colloutp (C) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
382 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
383 ## 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
|
384 ii = numel (C); |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
385 while (ii > 1) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
386 clss1 = class (C{ii}); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
387 jj = ii; |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
388 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
|
389 ## 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
|
390 --jj; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
391 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
392 if (jj < ii) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
393 ## Concatenate columns into current column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
394 C{jj} = [C{jj : ii}]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
395 ## 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
|
396 C(jj+1 : ii) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
397 ii = jj - 1; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
398 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
399 ## 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
|
400 --ii; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
401 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
402 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
403 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
404 endfunction |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
405 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
406 |
11141 | 407 %!test |
408 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12"; | |
409 %! fmtstr = "%f %d %f %s"; | |
410 %! 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
|
411 %! assert (isequal (c{1}, [1;5])); |
11141 | 412 %! 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
|
413 %! assert (iscellstr (c{4})); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
414 %! assert (isequal (c{3}, [3; -Inf])); |
11141 | 415 |
416 %!test | |
417 %! b = [10:10:100]; | |
418 %! b = [b; 8*b/5]; | |
419 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b); | |
420 %! fmt = "%f miles/hr = %f kilometers/hr"; | |
421 %! c = textscan (str, fmt); | |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
422 %! 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
|
423 %! 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
|
424 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
425 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
426 %! 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
|
427 %! 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
|
428 %! 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
|
429 %! 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
|
430 %! 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
|
431 %! 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
|
432 %! assert (a{5}, [25; 6]); |
11141 | 433 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
434 %!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
435 %! 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
|
436 %! 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
|
437 %! 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
|
438 %! 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
|
439 %! 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
|
440 %! 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
|
441 %! 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
|
442 %! 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
|
443 %! assert (a{3}', int32 ([16 241 3 0])); |
11141 | 444 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
445 ## Test with default endofline parameter |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
446 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
447 %! c = textscan ("L1\nL2", "%s"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
448 %! assert (c{:}, {"L1"; "L2"}); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
449 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
450 ## 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
|
451 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
452 %! 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
|
453 %! 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
|
454 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
455 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
456 %! ## 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
|
457 %! str = "Text1Text2Text\nTextText4Text\nText57Text"; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
458 %! c = textscan (str, "Text%*dText%dText"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
459 %! 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
|
460 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
461 ## CollectOutput test |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
462 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
463 %! b = [10:10:100]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
464 %! 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
|
465 %! 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
|
466 %! 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
|
467 %! 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
|
468 %! 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
|
469 %! 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
|
470 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
471 ## CollectOutput test with uneven column length files |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
472 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
473 %! b = [10:10:100]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
474 %! 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
|
475 %! 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
|
476 %! 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
|
477 %! 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
|
478 %! 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
|
479 %! 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
|
480 %! 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
|
481 %! 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
|
482 %! 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
|
483 %! 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
|
484 %! 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
|
485 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
486 ## Test input validation |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
487 %!error textscan () |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
488 %!error textscan (single (4)) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
489 %!error textscan ({4}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
490 %!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
|
491 %!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
|
492 %!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
|
493 |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
494 %! Test incomplete first data line |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
495 %! 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
|
496 %! 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
|
497 %! 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
|
498 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
499 ## bug #37023 (actually a strread test) |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
500 %!test |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
501 %! 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
|
502 %! 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
|
503 %! 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
|
504 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
505 ## Whitespace test (bug #37333) using delimiter ";" |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
506 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
507 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
508 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
509 %! 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
|
510 %! 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
|
511 %! 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
|
512 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
513 %! c = textscan (string, "%s", "delimiter", ";"); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
514 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
515 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
516 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
517 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
518 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
519 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
520 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
521 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
522 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
523 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
524 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
525 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
526 %! 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
|
527 %! 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
|
528 %! 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
|
529 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
530 %! 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
|
531 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
532 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
533 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
534 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
535 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
536 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
537 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
538 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
539 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
540 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
541 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
542 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
543 %! 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
|
544 %! 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
|
545 %! tc{1, end+1} = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
546 %! 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
|
547 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
548 %! 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
|
549 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
550 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
551 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
552 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
553 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
554 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
555 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
556 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
557 ## Whitespace test (bug #37333) whitespace "" arg |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
558 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
559 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
560 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
561 %! 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
|
562 %! 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
|
563 %! 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
|
564 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
565 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", ""); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
566 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
567 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
568 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
569 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
570 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
571 %! end |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
572 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
573 ## Whitespace test (bug #37333), whitespace " " arg |
15868
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
574 %!test |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
575 %! tc = []; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
576 %! tc{1, 1} = "C:/code;"; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
577 %! 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
|
578 %! 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
|
579 %! 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
|
580 %! string = [tc{:}]; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
581 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " "); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
582 %! for k = 1:numel (c{1}) |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
583 %! lh = c{1}{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
584 %! rh = tc{k}; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
585 %! rh(rh == ";") = ""; |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
586 %! rh = strtrim (rh); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
587 %! assert (strcmp (lh, rh)); |
6251fa48d28b
textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents:
15263
diff
changeset
|
588 %! end |
16060
f837bdd535f7
textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
15872
diff
changeset
|
589 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
590 ## Test reading from a real file |
16305
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
591 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
592 %! f = tempname (); |
16305
23636765e00a
textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16060
diff
changeset
|
593 %! 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
|
594 %! 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
|
595 %! 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
|
596 %! 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
|
597 %! 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
|
598 %! 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
|
599 %! 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
|
600 %! 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
|
601 %! 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
|
602 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
603 ## Tests reading with empty format, should return proper nr of columns |
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
|
604 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
605 %! f = tempname (); |
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
|
606 %! 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
|
607 %! 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
|
608 %! 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
|
609 %! 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
|
610 %! 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
|
611 %! 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
|
612 %! 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
|
613 %! 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
|
614 %! 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
|
615 %! 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
|
616 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
617 ## Tests reading with empty format; empty fields & incomplete lower row |
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
|
618 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
619 %! f = tempname (); |
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
|
620 %! 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
|
621 %! 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
|
622 %! 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
|
623 %! 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
|
624 %! 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
|
625 %! 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
|
626 %! 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
|
627 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
628 ## Error message tests |
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
|
629 |
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 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
631 %! f = tempname (); |
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
|
632 %! 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
|
633 %! 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
|
634 %! 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
|
635 %! 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
|
636 %! 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
|
637 %! 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
|
638 %! 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
|
639 %! 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
|
640 |
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 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
642 %! f = tempname (); |
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
|
643 %! 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
|
644 %! 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
|
645 %! 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
|
646 %! 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
|
647 %! 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
|
648 %! 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
|
649 %! 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
|
650 %! 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
|
651 |
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 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
653 %! f = tempname (); |
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
|
654 %! 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
|
655 %! 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
|
656 %! 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
|
657 %! 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
|
658 %! 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
|
659 %! 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
|
660 %! 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
|
661 %! 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
|
662 %! 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
|
663 %! 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
|
664 |
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 %!test |
19470
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
19241
diff
changeset
|
666 %! f = tempname (); |
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
|
667 %! 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
|
668 %! 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
|
669 %! 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
|
670 %! 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
|
671 %! 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
|
672 %! 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
|
673 %! 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
|
674 %! 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
|
675 %! 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
|
676 %! assert (msg1, lasterr); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17336
diff
changeset
|
677 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
678 ## Bug #41824 |
18717
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
679 %!test |
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
680 %! assert (textscan ("123", "", "whitespace", " "){:}, 123); |
2ce1f1966fc3
textscan.m: Fix undefined whitespace error with empty format input (bug #41824).
Sergey Plotnikov
parents:
17338
diff
changeset
|
681 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
682 ## Bug #42343-1, just test supplied emptyvalue (actually done by strread.m) |
18989
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
683 %!test |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
684 %! 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
|
685 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
686 ## Bug #42343-2, test padding with supplied emptyvalue (done by textscan.m) |
18989
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
687 %!test |
8354510f95ab
textscan.m: properly process "emptyvalue" (bug #42343).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
18717
diff
changeset
|
688 %! 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
|
689 %! 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
|
690 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
691 ## Bug #42528 |
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
|
692 %!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
|
693 %! 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
|
694 %! 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
|
695 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19793
diff
changeset
|
696 ## Illegal format specifiers |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
697 %!test |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19199
diff
changeset
|
698 %!error <no valid format conversion specifiers> textscan ("1.0", "%z"); |