Mercurial > octave-nkf
annotate scripts/io/textread.m @ 20255:481fa65e5cab stable
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
* textread.m: Clarify docstring describing format repeat count argument
Add examples
Fix bug occurring when last read line has no trailing EOL
Add tests
* textcan.m: Fix bug occurring when last read line has no trailing EOL
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Sun, 24 May 2015 22:47:55 +0200 |
parents | 935832827f47 |
children | 8a25649b9c77 |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19630
diff
changeset
|
1 ## Copyright (C) 2009-2015 Eric Chassande-Mottin, CNRS (France) |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
2 ## |
11104 | 3 ## This file is part of Octave. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
4 ## |
11104 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
14 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
11104 | 16 ## along with Octave; see the file COPYING. If not, see |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
18 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10627
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10627
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{n}) |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{prop1}, @var{value1}, @dots{}) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{n}, @var{prop1}, @var{value1}, @dots{}) |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
25 ## Read data from a text file. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
26 ## |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9746
diff
changeset
|
27 ## The file @var{filename} is read and parsed according to @var{format}. The |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10627
diff
changeset
|
28 ## function behaves like @code{strread} except it works by parsing a file |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
29 ## instead of a string. See the documentation of @code{strread} for details. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
30 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
31 ## In addition to the options supported by @code{strread}, this function |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
32 ## supports two more: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
33 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
34 ## @itemize |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
35 ## @item @qcode{"headerlines"}: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
36 ## The first @var{value} number of lines of @var{filename} are skipped. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
37 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
38 ## @item @qcode{"endofline"}: |
20136
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
39 ## Specify a single character or |
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
40 ## @qcode{"@xbackslashchar{}r@xbackslashchar{}n"}. If no value is given, it |
20195
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20136
diff
changeset
|
41 ## will be inferred from the file. If set to @qcode{""} (empty string) EOLs |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20136
diff
changeset
|
42 ## are ignored as delimiters. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
43 ## @end itemize |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
44 ## |
20255
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
45 ## The optional input @var{n} (format repeat count) specifies the number of |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
46 ## times the format string is to be used or the number of lines to be read, |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
47 ## whichever happens first while reading. The former is equivalent to |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
48 ## requesting that the data output vectors should be of length @var{N}. |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
49 ## Note that when reading files with format strings referring to multiple |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
50 ## lines, @var{n} should rather be the number of lines to be read than the |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
51 ## number of format string uses. |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
52 ## |
20195
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20136
diff
changeset
|
53 ## If the format string is empty (not just omitted) and the file contains only |
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
|
54 ## numeric data (excluding headerlines), textread will return a rectangular |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
55 ## matrix with the number of columns matching the number of numeric fields on |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16357
diff
changeset
|
56 ## the first data line of the file. Empty fields are returned as zero values. |
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
|
57 ## |
20255
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
58 ## Examples: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
59 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
60 ## @example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
61 ## Assume a data file like: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
62 ## 1 a 2 b |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
63 ## 3 c 4 d |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
64 ## 5 e |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
65 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
66 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
67 ## @example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
68 ## [a, b] = textread (f, "%f %s") |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
69 ## returns two columns of data, one with doubles, the other a |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
70 ## cellstr array: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
71 ## a = [1; 2; 3; 4; 5]ยด |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
72 ## b = {"a"; "b"; "c"; "d"; "e"} |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
73 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
74 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
75 ## @example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
76 ## [a, b] = textread (f, "%f %s", 3) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
77 ## (read data into two culumns, try to use the format string |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
78 ## three times) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
79 ## returns |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
80 ## a = [1; 2; 3]ยด |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
81 ## b = {"a"; "b"; "c"} |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
82 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
83 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
84 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
85 ## @example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
86 ## With a data file like: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
87 ## 1 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
88 ## a |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
89 ## 2 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
90 ## b |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
91 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
92 ## [a, b] = textread (f, "%f %s", 2) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
93 ## returns a = 1 and b = {"a"}; i.e., the format string is used |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
94 ## only once because the format string refers to 2 lines of the |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
95 ## data file. To obtain 2x1 data output columns, specify N = 4 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
96 ## (number of data lines containing all requested data) rather |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
97 ## than 2. |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
98 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
99 ## |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
100 ## @seealso{strread, load, dlmread, fscanf, textscan} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
101 ## @end deftypefn |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
102 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
103 function varargout = textread (filename, format = "%f", varargin) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
104 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
105 BUFLENGTH = 4096; # Read buffer to speed up processing @var{n} |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
106 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
107 ## Check input |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
108 if (nargin < 1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
109 print_usage (); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
110 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
111 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
112 if (! ischar (filename) || ! ischar (format)) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
113 error ("textread: FILENAME and FORMAT arguments must be strings"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
114 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
115 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
116 if (! isempty (varargin) && isnumeric (varargin{1})) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
117 nlines = varargin{1}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
118 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
119 nlines = Inf; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
120 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
121 if (nlines < 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
122 printf ("textread: N = 0, no data read\n"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
123 varargout = cell (1, nargout); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
124 return; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
125 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
126 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
127 ## Read file |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
128 fid = fopen (filename, "r"); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
129 if (fid == -1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
130 error ("textread: could not open '%s' for reading", filename); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
131 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
132 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
133 ## Skip header lines if requested |
9828 | 134 headerlines = find (strcmpi (varargin, "headerlines"), 1); |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
135 if (! isempty (headerlines)) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
136 ## Beware of missing or wrong headerline value |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
137 if (headerlines == numel (varargin) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
138 || ! isnumeric (varargin{headerlines + 1})) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
139 error ("missing or illegal value for 'headerlines'" ); |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
140 endif |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
141 ## Avoid conveying floats to fskipl |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
142 varargin{headerlines + 1} = round (varargin{headerlines + 1}); |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
143 ## Beware of zero valued headerline, fskipl would skip to EOF |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
144 if (varargin{headerlines + 1} > 0) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
145 fskipl (fid, varargin{headerlines + 1}); |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
146 varargin(headerlines:headerlines+1) = []; |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
147 nargin = nargin - 2; |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
148 elseif (varargin{headerlines + 1} < 0) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
149 warning ("textread: negative headerline value ignored"); |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
150 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
151 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
152 st_pos = ftell (fid); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
153 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
154 ## Read a first file chunk. Rest follows after endofline processing |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
155 [str, count] = fscanf (fid, "%c", BUFLENGTH); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
156 if (isempty (str) || count < 1) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
157 warning ("textread: empty file"); |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
158 varargout = cell (1, nargout); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
159 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
160 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
161 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
162 endofline = find (strcmpi (varargin, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
163 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12880
diff
changeset
|
164 ## 'endofline' option set by user. |
20239
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
165 if (ischar (varargin{endofline + 1})) |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
166 eol_char = varargin{endofline + 1}; |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
167 if (! any (strcmp (eol_char, {"", "\n", "\r", "\r\n"}))) |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
168 error ("textscan: illegal EndOfLine character value specified"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
169 endif |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
170 else |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
171 error ("character value required for EndOfLine"); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
172 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
173 else |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
174 ## Determine EOL from file. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
175 ## Search for EOL candidates in the first BUFLENGTH chars |
20255
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
176 ## FIXME Ignore risk of 2-byte EOL (\r\n) being split at exactly BUFLENGTH |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
177 eol_srch_len = min (length (str), BUFLENGTH); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
178 ## 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:
16059
diff
changeset
|
179 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:
12866
diff
changeset
|
180 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
181 ## 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:
16059
diff
changeset
|
182 elseif (! isempty (strfind (str(1 : eol_srch_len), "\r"))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
183 eol_char = "\r"; |
16311
9c4ac8f25a8c
textscan.m, textread.m: fix wrong code assessing EOL char(s), remove duplicate code
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16059
diff
changeset
|
184 ## Otherwise, use plain *nix (LF) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
185 else |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
186 eol_char = "\n"; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
187 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
188 ## Set up default endofline param value |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
189 varargin(end+1:end+2) = {"endofline", eol_char}; |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
190 endif |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
191 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
192 ## Now that we know what EOL looks like, we can process format_repeat_count. |
18891
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
193 ## FIXME: The below isn't ML-compatible: counts lines, not format string uses |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
194 if (isfinite (nlines) && (nlines > 0)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
195 l_eol_char = length (eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
196 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
197 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
198 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
199 ## 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:
14430
diff
changeset
|
200 while (n_eoi < nlines && count == BUFLENGTH) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
201 [nstr, count] = fscanf (fid, "%c", BUFLENGTH); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
202 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
203 ## 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:
14430
diff
changeset
|
204 if (l_eol_char > 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
205 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:
14430
diff
changeset
|
206 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
207 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
208 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
209 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
210 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
211 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
212 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
213 endwhile |
20255
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
214 ## Handle case of missing or incomplete trailing EOL |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
215 if (! strcmp (str(end - length (eol_char) + 1 : end), eol_char)) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
216 eoi = [ eoi (length (str)) ]; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
217 ++n_eoi; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
218 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
219 ## 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:
14430
diff
changeset
|
220 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
221 eoi_pos = nblks * BUFLENGTH + count; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
222 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
223 eoi_pos = (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:
14430
diff
changeset
|
224 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
225 fseek (fid, st_pos, "bof"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
226 str = fscanf (fid, "%c", eoi_pos); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
227 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
228 fseek (fid, st_pos, "bof"); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
229 str = fread (fid, "char=>char").'; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
230 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
231 fclose (fid); |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
232 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
233 ## Set up default whitespace param value if needed |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
234 if (isempty (find (strcmpi ("whitespace", varargin)))) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
235 varargin(end+1:end+2) = {"whitespace", " \b\t"}; |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
236 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
237 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
238 ## Call strread to make it do the real work |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
239 [varargout{1:max (nargout, 1)}] = strread (str, format, varargin{:}); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
240 |
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
|
241 ## Hack to concatenate/reshape numeric output into 2D array (undocumented ML) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
242 ## In ML this only works in case of an 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
|
243 if (isempty (format)) |
19627
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
244 ## Get number of fields per line. |
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
|
245 ## 1. Get eol_char position |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
246 iwhsp = find (strcmpi ("whitespace", varargin)); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
247 whsp = varargin{iwhsp + 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
|
248 idx = regexp (str, eol_char, "once"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
249 ## 2. Get first data line til EOL. Avoid corner case of just one 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
|
250 if (! isempty (idx)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
251 str = str(1:idx-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
|
252 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
|
253 idelimiter = find (strcmpi (varargin, "delimiter"), 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
|
254 if (isempty (idelimiter)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
255 ## Assume 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
|
256 ## 3A. whitespace incl. consecutive whitespace => single space |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
257 str = regexprep (str, sprintf ("[%s]+", whsp), ' '); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
258 ## 4A. Remove possible leading & trailing spaces |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
259 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
|
260 ## 5A. Count spaces, add one to get nr of data fields per 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
|
261 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
|
262 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
|
263 ## 3B. Just count delimiters. FIXME: delimiters could occur in literals |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
264 delimiter = varargin{idelimiter+1}; |
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
|
265 ncols = numel (regexp (str, sprintf ("[%s]", delimiter))) + 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
|
266 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
|
267 ## 6. Reshape; watch out, we need a transpose |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
268 nrows = ceil (numel (varargout{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
|
269 pad = mod (numel (varargout{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
|
270 if (pad > 0) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
271 pad = ncols - 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
|
272 varargout{1}(end+1 : end+pad) = NaN; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
273 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
|
274 varargout{1} = reshape (varargout{1}, ncols, nrows)'; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
275 ## ML replaces empty values with NaNs |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
276 varargout{1}(find (isnan (varargout{1}))) = 0; |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
277 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
|
278 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
279 endfunction |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
280 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
281 |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
282 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
283 %! f = tempname (); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
284 %! d = rand (5, 3); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
285 %! dlmwrite (f, d, "precision", "%5.2f"); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
286 %! [a, b, c] = textread (f, "%f %f %f", "delimiter", ",", "headerlines", 3); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
287 %! unlink (f); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
288 %! assert (a, d(4:5, 1), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
289 %! assert (b, d(4:5, 2), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
290 %! assert (c, d(4:5, 3), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
291 |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
292 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
293 %! f = tempname (); |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
294 %! d = rand (7, 2); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
295 %! dlmwrite (f, d, "precision", "%5.2f"); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
296 %! [a, b] = textread (f, "%f, %f", "headerlines", 1); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
297 %! unlink (f); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
298 %! assert (a, d(2:7, 1), 1e-2); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
299 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
300 ## Test reading 2D matrix with empty 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
|
301 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
302 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
303 %! d = rand (5, 2); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
304 %! dlmwrite (f, d, "precision", "%5.2f"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
305 %! A = textread (f, "", "headerlines", 3); |
0cbe330f39a2
textscan.m, 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 %! 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
|
307 %! assert (A, d(4:5, :), 1e-2); |
0cbe330f39a2
textscan.m, 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 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
309 ## Read multiple lines using empty format string |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
310 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
311 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
312 %! 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
|
313 %! 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
|
314 %! d = rand (1, 4); |
0cbe330f39a2
textscan.m, 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 %! fprintf (fid, " %f %f %f %f ", d); |
0cbe330f39a2
textscan.m, 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 %! 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
|
317 %! A = textread (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
|
318 %! 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
|
319 %! assert (A, d, 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
|
320 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
321 ## Empty format, corner case = one line w/o EOL |
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
|
322 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
323 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
324 %! 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
|
325 %! 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
|
326 %! d = rand (1, 4); |
0cbe330f39a2
textscan.m, 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 %! fprintf (fid, " %f %f %f %f ", d); |
0cbe330f39a2
textscan.m, 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 %! 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
|
329 %! A = textread (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
|
330 %! 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
|
331 %! assert (A, d, 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
|
332 |
20255
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
333 ## Tests with format repeat count #1 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
334 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
335 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
336 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
337 %! fprintf (fid, "%2d %s %2d %s\n %2d %s %2d %s \n", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
338 %! 10, "a", 20, "b", 30, "c", 40, "d"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
339 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
340 %! [a, b] = textread (f, "%d %s", 1); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
341 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
342 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
343 %! [a, b] = textread (f, "%d %s", 2); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
344 %! assert (a, int32 ([10; 20])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
345 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
346 %! [a, b] = textread (f, "%d %s", 3); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
347 %! assert (a, int32 ([10; 20; 30])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
348 %! assert (b, {"a"; "b"; "c"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
349 %! [a, b] = textread (f, "%d %s", 4); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
350 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
351 %! assert (b, {"a"; "b"; "c"; "d"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
352 %! [a, b] = textread (f, "%d %s", 5); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
353 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
354 %! assert (b, {"a"; "b"; "c"; "d"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
355 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
356 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
357 ## Tests with format repeat count #2, missing last EOL |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
358 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
359 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
360 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
361 %! fprintf (fid, "%2d %s %2d %s\n %2d %s %2d %s", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
362 %! 10, "a", 20, "b", 30, "c", 40, "d"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
363 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
364 %! [a, b] = textread (f, "%d %s", 1); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
365 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
366 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
367 %! [a, b] = textread (f, "%d %s", 2); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
368 %! assert (a, int32 ([10; 20])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
369 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
370 %! [a, b] = textread (f, "%d %s", 3); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
371 %! assert (a, int32 ([10; 20; 30])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
372 %! assert (b, {"a"; "b"; "c"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
373 %! [a, b] = textread (f, "%d %s", 4); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
374 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
375 %! assert (b, {"a"; "b"; "c"; "d"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
376 %! [a, b] = textread (f, "%d %s", 5); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
377 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
378 %! assert (b, {"a"; "b"; "c"; "d"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
379 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
380 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
381 ## Tests with format repeat count #3, incomplete last line |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
382 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
383 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
384 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
385 %! fprintf (fid, "%2d %s %2d %s\n %2d %s %2d", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
386 %! 10, "a", 20, "b", 30, "c", 40); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
387 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
388 %! [a, b] = textread (f, "%d %s", 1); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
389 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
390 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
391 %! [a, b] = textread (f, "%d %s", 2); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
392 %! assert (a, int32 ([10; 20])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
393 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
394 %! [a, b] = textread (f, "%d %s", 3); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
395 %! assert (a, int32 ([10; 20; 30])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
396 %! assert (b, {"a"; "b"; "c"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
397 %! [a, b] = textread (f, "%d %s", 4); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
398 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
399 %! assert (b, {"a"; "b"; "c"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
400 %! [a, b] = textread (f, "%d %s", 5); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
401 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
402 %! assert (b, {"a"; "b"; "c"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
403 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
404 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
405 ## Tests with format repeat count #4, incomplete last line but with trailing EOL |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
406 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
407 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
408 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
409 %! fprintf (fid, "%2d %s %2d %s\n %2d %s %2d\n", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
410 %! 10, "a", 20, "b", 30, "c", 40); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
411 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
412 %! [a, b] = textread (f, "%d %s", 4); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
413 %! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
414 %! assert (b, {"a"; "b"; "c"; ""}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
415 #%! [a, b] = textread (f, "%d %s", 5); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
416 #%! assert (a, int32 ([10; 20; 30; 40])); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
417 #%! assert (b, {"a"; "b"; "c"; ""}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
418 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
419 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
420 ## Tests with format repeat count #5, nr of data lines = limiting factor |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
421 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
422 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
423 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
424 %! fprintf (fid, "%2d\n%s\n%2dn%s", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
425 %! 1, "a", 2, "b"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
426 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
427 %! [a, b] = textread (f, "%d %s", 2); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
428 %! assert (a, int32 (1)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
429 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20239
diff
changeset
|
430 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
431 ## Read multiple lines using empty format string, missing data (should be 0) |
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
|
432 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
433 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
434 %! 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
|
435 %! 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
|
436 %! d = rand (1, 4); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
437 %! fprintf (fid, "%f, %f, , %f, %f ", d); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
438 %! 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
|
439 %! A = textread (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
|
440 %! 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
|
441 %! assert (A, [ d(1:2) 0 d(3:4)], 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
|
442 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
443 ## Test with empty positions - ML returns 0 for empty fields |
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
|
444 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
445 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
446 %! 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
|
447 %! 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
|
448 %! d = rand (1, 4); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
449 %! fprintf (fid, ",2,,4\n5,,7,\n"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
450 %! 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
|
451 %! A = textread (f, "", "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
|
452 %! 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
|
453 %! assert (A, [0 2 0 4; 5 0 7 0], 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
|
454 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
455 ## Another test with empty format + positions, now with more incomplete lower |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
456 ## row (must be appended with zeros to get rectangular matrix) |
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
|
457 %!test |
19312
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18891
diff
changeset
|
458 %! f = tempname (); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
459 %! 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
|
460 %! 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
|
461 %! d = rand (1, 4); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
462 %! fprintf (fid, ",2,,4\n5,\n"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
463 %! 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
|
464 %! A = textread (f, "", "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
|
465 %! 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
|
466 %! assert (A, [0 2 0 4; 5 0 0 0], 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
|
467 |
20239
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
468 ## Test endofline |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
469 %!test |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
470 %! f = tempname (); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
471 %! fid = fopen (f, "w"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
472 %! fprintf (fid, "a\rb\rc"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
473 %! fclose (fid); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
474 %! ## Test EOL detection |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
475 %! d = textread (f, "%s"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
476 %! assert (d, {"a";"b";"c"}); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
477 %! ## Test explicit EOL specification (bug #45046) |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
478 %! d = textread (f, "%s", "endofline", "\r"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
479 %! assert (d, {"a"; "b"; "c"}); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
480 %! unlink (f); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20195
diff
changeset
|
481 |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
482 ## Test input validation |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
483 %!error textread () |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
484 %!error textread (1) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
485 %!error <arguments must be strings> textread (1, "%f") |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
486 %!error <arguments must be strings> textread ("fname", 1) |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
487 %!error <missing or illegal value for> textread (file_in_loadpath ("textread.m"), "", "headerlines") |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
488 %!error <missing or illegal value for> textread (file_in_loadpath ("textread.m"), "", "headerlines", 'hh') |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
489 %!error <character value required for> textread (file_in_loadpath ("textread.m"), "%s", "endofline", true) |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
490 |