annotate scripts/io/textscan.m @ 16357:0cbe330f39a2

textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
author Philip Nienhuis <prnienhuis@users.sf.net>
date Fri, 22 Mar 2013 17:46:04 +0100
parents 9c4ac8f25a8c
children e2de3c8882be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16060
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
1 ## Copyright (C) 2010-2013 Ben Abbott <bpabbott@mac.com>
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This file is part of Octave.
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ##
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## your option) any later version.
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## General Public License for more details.
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
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
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## @itemize
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
37 ## @item "collectoutput":
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
38 ## A value of 1 or true instructs textscan to concatenate consecutive columns
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
39 ## of the same class in the output cell array. A value of 0 or false (default)
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
40 ## leaves output in distinct columns.
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
41 ##
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
42 ## @item "endofline":
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
43 ## Specify "\r", "\n" or "\r\n" (for CR, LF, or CRLF). If no value is given,
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
44 ## it will be inferred from the file. If set to "" (empty string) EOLs are
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
45 ## ignored as delimiters and added to whitespace.
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
46 ##
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
47 ## @item "headerlines":
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
48 ## The first @var{value} number of lines of @var{fid} are skipped.
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
49 ##
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
50 ## @item "returnonerror":
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
51 ## If set to numerical 1 or true (default), return normally when read errors
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
52 ## have been encountered. If set to 0 or false, return an error and no data.
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
53 ## 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
54 ## 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
55 ## may have little or no actual effect.
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 ## @end itemize
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 ##
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
58 ## 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
59 ## 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
60 ## 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
61 ## 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
62 ## in this sense it differs slightly from the format repeat count in strread.
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 ##
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
64 ## 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
65 ## by the number of format specifiers.
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 ##
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 ## The second output, @var{position}, provides the position, in characters,
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 ## from the beginning of the file.
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 ##
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
70 ## 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
71 ## 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
72 ## 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
73 ## 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
74 ##
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 ## @seealso{dlmread, fscanf, load, strread, textread}
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 ## @end deftypefn
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
78 function [C, position] = textscan (fid, format = "%f", varargin)
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
80 BUFLENGTH = 4096; ## Read buffer
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
81 emptfmt = 0; ## Signals deliberately empty format 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
82
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 ## Check input
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 if (nargin < 1)
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85 print_usage ();
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
86 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
87
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
88 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
89 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
90 format = "%f";
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 endif
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
93 if (! ischar (format))
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
94 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
95 endif
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
96
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
97 ## Determine the number of data fields & initialize output array
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
98 num_fields = numel (strfind (format, "%")) - numel (strfind (format, "%*"));
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
99 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
100
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
101 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
102 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
103 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
104
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
105 args = varargin;
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
106 if (nargin > 2 && isnumeric (args{1}))
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
107 nlines = args{1};
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 else
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 nlines = Inf;
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 endif
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
111 if (nlines < 1)
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
112 printf ("textscan: N = 0, no data read\n");
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
113 return
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
114 endif
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 if (! any (strcmpi (args, "emptyvalue")))
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 ## Matlab returns NaNs for missing values
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
118 args(end+1:end+2) = {'emptyvalue', NaN};
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
119 endif
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
121 ## 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
122
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
123 ipos = find (strcmpi (args, "whitespace"));
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
124 if (isempty (ipos))
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
125 ## Matlab default whitespace = " \b\t"
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
126 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
127 whitespace = " \b\t";
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
128 else
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
129 ## 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
130 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
131 ## 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
132 ## 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
133 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
134 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
135 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
136 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
137
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
138 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
139 ## 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
140 ## strread() will pick this up further
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
141 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
142 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
143 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
144 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
145 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
146
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
147 collop = false;
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
148 ipos = find (strcmpi (args, "collectoutput"));
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
149 if (! isempty (ipos))
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
150 ## 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
151 if (isscalar (args{ipos+1})
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
152 && (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
153 collop = args{ipos+1};
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
154 else
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
155 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
156 endif
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
157 ## 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
158 args(ipos:ipos+1) = [];
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
159 endif
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
160
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
161 if (any (strcmpi (args, "returnonerror")))
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
162 ## 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
163 ## 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
164 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
165 else
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
166 ## Set default value (=true)
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
167 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
168 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
169
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
170 ## 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
171 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
172 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
173 ## 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
174 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
175 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
176 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
177 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
178
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
179 if (ischar (fid))
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
180 ## Read from a text string
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
181 if (nargout == 2)
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
182 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
183 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
184 str = fid;
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
185 else
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
186 st_pos = ftell (fid);
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
187 ## 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
188 if (! isempty (headerlines))
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
189 ## 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
190 if (headerlines == numel (args)
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
191 || ! isnumeric (args{headerlines + 1}))
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
192 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
193 endif
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
194 ## Avoid conveying floats to fskipl
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
195 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
196 if (args{headerlines + 1} > 0)
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
197 ## 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
198 fskipl (fid, args{headerlines + 1});
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
199 args(headerlines:headerlines+1) = [];
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
200 st_pos = ftell (fid);
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
201 elseif (args{headerlines + 1} < 0)
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
202 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
203 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
204 endif
23636765e00a textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16060
diff changeset
205 ## 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
206 [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
207
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
208 endif
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
209
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
210 ## Check for empty result
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
211 if (isempty (str))
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
212 warning ("textscan: no data read");
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
213 return;
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
214 endif
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
215
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
216 ## 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
217 endofline = find (strcmpi (args, "endofline"), 1);
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
218 if (! isempty (endofline))
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12916
diff changeset
219 if (ischar (args{endofline + 1}))
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
220 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
221 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
222 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
223 endif
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
224 else
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12916
diff changeset
225 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
226 endif
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
227 else
15868
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
228 if (! ischar (fid))
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
229 ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
230 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
231 ## 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
232 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
233 eol_char = "\r\n";
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
234 ## 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
235 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
236 eol_char = "\r";
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
237 ## Otherwise, use plain UNIX (LF)
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
238 else
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
239 eol_char = "\n";
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
240 endif
15868
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
241 else
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
242 eol_char = "\n";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
243 endif
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
244 ## 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
245 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
246 endif
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
247
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
248 if (!ischar (fid))
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
249 ## 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
250 ## 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
251 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
252 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
253 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
254 n_eoi = length (eoi);
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
255 nblks = 0;
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
256 ## 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
257 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
258 [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
259 if (count > 0)
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
260 ## 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
261 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
262 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
263 else
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
264 str = nstr;
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
265 endif
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
266 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
267 n_eoi += numel (eoi);
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
268 ++nblks;
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
269 endif
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
270 endwhile
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
271 ## 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
272 if (isempty (eoi))
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
273 printf ("textscan: format repeat count specified but no endofline found\n");
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
274 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
275 else
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
276 ## Compute data size to read incl complete EOL
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
277 data_size = (nblks * BUFLENGTH) + eoi(end + min (nlines, n_eoi) - n_eoi) \
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
278 + l_eol_char - 1;
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
279 endif
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
280 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
281 str = fscanf (fid, "%c", data_size);
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
282 else
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
283 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
284 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
285 endif
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
286 endif
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
287
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
288 ## 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
289 ## 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
290 ## 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
291 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
292 if (collop || emptfmt)
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
293 if (! eol_at_end)
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
294 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
295 endif
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
296 elseif (eol_at_end)
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
297 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
298 ## 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
299 if (isempty (str)); return; endif
a922f768ee09 textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14799
diff changeset
300 endif
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
301
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
302 ## 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
303 C = cell (1, num_fields);
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
304 [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
305
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 ## 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
307 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
308 ## 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
309 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
310 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
311 ## 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
312 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
313 ## 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
314 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
315 if (pad)
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
316 ## Textscan returns NaNs for empty fields
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
317 C(1) = [C{1}; NaN(ncols - pad, 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
318 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
319 ## Replace NaNs with EmptyValue, if any
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 ipos = find (strcmpi (args, "emptyvalue"));
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 (ipos)
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 C{1}(find (isnan (C{1}))) = args{ipos+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
323 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
324 ## 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
325 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
326 ## Reshape C; watch out, transpose needed
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 C(1) = reshape (C{1}, ncols, 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
328 ## 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
329 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
330 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
331 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
332 C{1} = C{1}(:, 1);
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
333 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
334 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
335
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
336 ## 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
337 if (collop)
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
338 C = colloutp (C);
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
339 endif
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
340
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
341 if (nargout == 2)
14565
98aaebc56d7c 2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents: 14552
diff changeset
342 ## Remember file position (persistent var)
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
343 position = ftell (fid);
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
344 endif
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
345
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
346 endfunction
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
347
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
348
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
349 ## 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
350 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
351
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
352 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
353 ## 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
354 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
355 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
356 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
357 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
358 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
359 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
360
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
361 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
362
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
363
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
364 ## 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
365 function C = colloutp (C)
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
366
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
367 ## 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
368 ii = numel (C);
14552
86854d032a37 maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
369 while (ii > 1)
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
370 clss1 = class (C{ii});
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
371 jj = ii;
14552
86854d032a37 maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
372 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
373 ## 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
374 --jj;
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
375 endwhile
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
376 if (jj < ii)
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
377 ## Concatenate columns into current column
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
378 C{jj} = [C{jj : ii}];
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
379 ## 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
380 C(jj+1 : ii) = [];
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
381 ii = jj - 1;
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
382 else
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
383 ## 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
384 --ii;
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
385 endif
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
386 endwhile
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
387
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
388 endfunction
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
389
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
390
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
391 %!test
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
392 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12";
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
393 %! fmtstr = "%f %d %f %s";
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
394 %! 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
395 %! assert (isequal (c{1}, [1;5]));
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
396 %! 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
397 %! assert (iscellstr (c{4}));
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
398 %! assert (isequal (c{3}, [3; -Inf]));
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
399
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
400 %!test
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
401 %! b = [10:10:100];
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
402 %! b = [b; 8*b/5];
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
403 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b);
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
404 %! fmt = "%f miles/hr = %f kilometers/hr";
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
405 %! c = textscan (str, fmt);
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
406 %! 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
407 %! 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
408
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
409 %!test
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
410 %! 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
411 %! 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
412 %! 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
413 %! 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
414 %! 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
415 %! 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
416 %! assert (a{5}, [25; 6]);
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
417
12866
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
418 %!test
fe6e2afcd9ee Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12679
diff changeset
419 %! 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
420 %! 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
421 %! 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
422 %! 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
423 %! 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
424 %! 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
425 %! 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
426 %! 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
427 %! assert (a{3}', int32 ([16 241 3 0]));
11141
224c80da37c5 textscan.m: Add new function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
428
12891
f5a3f77d51aa strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12880
diff changeset
429 %% Test with default endofline parameter
f5a3f77d51aa strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12880
diff changeset
430 %!test
f5a3f77d51aa strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12880
diff changeset
431 %! c = textscan ("L1\nL2", "%s");
f5a3f77d51aa strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12880
diff changeset
432 %! assert (c{:}, {"L1"; "L2"});
f5a3f77d51aa strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12880
diff changeset
433
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
434 %% 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
435 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
436 %! 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
437 %! 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
438
12914
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
439 %!test
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
440 %! # No delimiters at all besides EOL. Skip fields, even empty fields
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
441 %! str = "Text1Text2Text\nTextText4Text\nText57Text";
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
442 %! c = textscan (str, "Text%*dText%dText");
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
443 %! 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
444
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
445 %!test
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
446 %% CollectOutput test
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
447 %! b = [10:10:100];
ac00ae83ea2e More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 12891
diff changeset
448 %! 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
449 %! 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
450 %! 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
451 %! 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
452 %! 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
453 %! 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
454
14799
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
455 %!test
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
456 %% CollectOutput test with uneven column length files
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
457 %! b = [10:10:100];
21197c43f984 Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14621
diff changeset
458 %! 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
459 %! 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
460 %! 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
461 %! 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
462 %! 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
463 %! 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
464 %! 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
465 %! 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
466 %! 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
467 %! 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
468 %! 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
469
12877
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
470 %% Test input validation
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
471 %!error textscan ()
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
472 %!error textscan (single (4))
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
473 %!error textscan ({4})
ddea3962b024 Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents: 12873
diff changeset
474 %!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
475 %!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
476 %!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
477
14800
a922f768ee09 textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14799
diff changeset
478 %! Test incomplete first data line
a922f768ee09 textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14799
diff changeset
479 %! 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
480 %! 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
481 %! 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
482
2136343014d5 bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14868
diff changeset
483 %% bug #37023 (actually a strread test)
2136343014d5 bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14868
diff changeset
484 %!test
2136343014d5 bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 14868
diff changeset
485 %! 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
486 %! 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
487 %! 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
488
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
489 %% Whitespace test (bug #37333) using delimiter ";"
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
490 %!test
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
491 %! tc = [];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
492 %! tc{1, 1} = "C:/code;";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
493 %! 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
494 %! 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
495 %! 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
496 %! string = [tc{:}];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
497 %! c = textscan (string, "%s", "delimiter", ";");
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
498 %! for k = 1:numel (c{1})
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
499 %! lh = c{1}{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
500 %! rh = tc{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
501 %! rh(rh == ";") = "";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
502 %! rh = strtrim (rh);
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
503 %! assert (strcmp (lh, rh));
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
504 %! end
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
505
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
506 %% Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
507 %!test
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
508 %! tc = [];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
509 %! tc{1, 1} = "C:/code;";
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/meas;";
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/sim;;";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
512 %! 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
513 %! string = [tc{:}];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
514 %! 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
515 %! for k = 1:numel (c{1})
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
516 %! lh = c{1}{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
517 %! rh = tc{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
518 %! rh(rh == ";") = "";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
519 %! rh = strtrim (rh);
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
520 %! assert (strcmp (lh, rh));
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
521 %! end
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
522
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
523 %% Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
524 %!test
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
525 %! tc = [];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
526 %! tc{1, 1} = "C:/code;";
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/meas;";
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/sim;;";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
529 %! tc{1, end+1} = "";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
530 %! 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
531 %! string = [tc{:}];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
532 %! 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
533 %! for k = 1:numel (c{1})
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
534 %! lh = c{1}{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
535 %! rh = tc{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
536 %! rh(rh == ";") = "";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
537 %! rh = strtrim (rh);
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
538 %! assert (strcmp (lh, rh));
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
539 %! end
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
540
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
541 %% Whitespace test (bug #37333) whitespace "" arg
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
542 %!test
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
543 %! tc = [];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
544 %! tc{1, 1} = "C:/code;";
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
545 %! 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
546 %! 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
547 %! 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
548 %! string = [tc{:}];
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
549 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
550 %! for k = 1:numel (c{1})
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
551 %! lh = c{1}{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
552 %! rh = tc{k};
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
553 %! rh(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
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
557 %% Whitespace test (bug #37333), whitespace " " arg
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 %! rh = strtrim (rh);
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
571 %! assert (strcmp (lh, rh));
6251fa48d28b textscan.m: various whitespace & delimiter tests added
Andy Register <andy.register@gatech.edu>
parents: 15263
diff changeset
572 %! end
16060
f837bdd535f7 textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 15872
diff changeset
573
16305
23636765e00a textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16060
diff changeset
574 %% Test reading from a real file
23636765e00a textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16060
diff changeset
575 %!test
23636765e00a textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16060
diff changeset
576 %! f = tmpnam ();
23636765e00a textscan.m: re-enable reading from file id, test added (bug #38517)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16060
diff changeset
577 %! 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
578 %! 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
579 %! 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
580 %! 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
581 %! 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
582 %! 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
583 %! 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
584 %! 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
585 %! 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
586
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
587 %% Tests reading with empty format, should return proper nr of columns
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
588 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
589 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
590 %! 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
591 %! 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
592 %! 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
593 %! 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
594 %! 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
595 %! 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
596 %! 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
597 %! 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
598 %! 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
599 %! 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
600
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
601 %% Tests reading with empty format; empty fields & incomplete lower row
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
602 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
603 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
604 %! 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
605 %! 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
606 %! 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
607 %! 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
608 %! 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
609 %! 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
610 %! 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
611
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 %% Error message tests
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
613
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 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
615 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
616 %! 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
617 %! 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
618 %! 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
619 %! 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
620 %! 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
621 %! 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
622 %! 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
623 %! 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
624
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 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
626 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
627 %! 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
628 %! 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
629 %! 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
630 %! 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
631 %! 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
632 %! 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
633 %! 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
634 %! 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
635
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 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
637 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
638 %! 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
639 %! 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
640 %! 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
641 %! 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
642 %! 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
643 %! 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
644 %! 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
645 %! 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
646 %! 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
647 %! 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
648
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 %!test
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
650 %! f = tmpnam ();
0cbe330f39a2 textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 16311
diff changeset
651 %! 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
652 %! 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
653 %! 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
654 %! 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
655 %! 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
656 %! 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
657 %! 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
658 %! 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
659 %! 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
660 %! assert (msg1, lasterr);