Mercurial > octave
annotate scripts/legacy/textread.m @ 33631:d06912c91b7d default tip @
Fix typo in b54b4389e987.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Mon, 27 May 2024 16:45:06 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 ## Copyright (C) 2009-2024 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
7 ## |
11104 | 8 ## 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
|
9 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
11104 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
13 ## (at your option) any later version. |
11104 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22489
diff
changeset
|
18 ## GNU 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
|
19 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
11104 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
25 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
27 ## @deftypefn {} {[@var{a}, @dots{}] =} textread (@var{filename}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
28 ## @deftypefnx {} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
29 ## @deftypefnx {} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{n}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
30 ## @deftypefnx {} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{prop1}, @var{value1}, @dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
31 ## @deftypefnx {} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{n}, @var{prop1}, @var{value1}, @dots{}) |
25764
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
32 ## |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
33 ## This function is obsolete. Use @code{textscan} instead. |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
34 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
35 ## 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
|
36 ## |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9746
diff
changeset
|
37 ## 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
|
38 ## 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
|
39 ## 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
|
40 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
41 ## 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
|
42 ## supports two more: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
43 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
44 ## @itemize |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
45 ## @item @qcode{"headerlines"}: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
46 ## 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
|
47 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
48 ## @item @qcode{"endofline"}: |
20101
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
49 ## Specify a single character or |
29117
10a35049bad7
doc: Cleanup Texinfo macros use in documentation.
Rik <rik@octave.org>
parents:
28098
diff
changeset
|
50 ## @qcode{"@backslashchar{}r@backslashchar{}n"}. If no value is given, it |
20160
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20101
diff
changeset
|
51 ## 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:
20101
diff
changeset
|
52 ## are ignored as delimiters. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
53 ## @end itemize |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
54 ## |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
55 ## 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:
20204
diff
changeset
|
56 ## 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:
20204
diff
changeset
|
57 ## 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:
20204
diff
changeset
|
58 ## 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:
20204
diff
changeset
|
59 ## 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:
20204
diff
changeset
|
60 ## 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:
20204
diff
changeset
|
61 ## number of format string uses. |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
62 ## |
20160
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20101
diff
changeset
|
63 ## 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
|
64 ## 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
|
65 ## 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
|
66 ## 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
|
67 ## |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
68 ## Examples: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
69 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
70 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
71 ## @group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
72 ## 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:
20204
diff
changeset
|
73 ## 1 a 2 b |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
74 ## 3 c 4 d |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
75 ## 5 e |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
76 ## @end group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
77 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
78 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
79 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
80 ## @group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
81 ## [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:
20204
diff
changeset
|
82 ## 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:
20204
diff
changeset
|
83 ## cellstr array: |
20222
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
84 ## a = [1; 2; 3; 4; 5] |
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
85 ## b = @{"a"; "b"; "c"; "d"; "e"@} |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
86 ## @end group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
87 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
88 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
89 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
90 ## @group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
91 ## [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:
20204
diff
changeset
|
92 ## (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:
20204
diff
changeset
|
93 ## three times) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
94 ## returns |
20222
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
95 ## a = [1; 2; 3] |
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
96 ## b = @{"a"; "b"; "c"@} |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
97 ## |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
98 ## @end group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
99 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
100 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
101 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
102 ## @group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
103 ## 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:
20204
diff
changeset
|
104 ## 1 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
105 ## a |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
106 ## 2 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
107 ## b |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
108 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
109 ## [a, b] = textread (f, "%f %s", 2) |
20222
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
110 ## returns a = 1 and b = @{"a"@}; i.e., the format string is used |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
111 ## only once because the format string refers to 2 lines of the |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
112 ## data file. To obtain 2x1 data output columns, specify N = 4 |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
113 ## (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:
20204
diff
changeset
|
114 ## than 2. |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
115 ## @end group |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
116 ## @end example |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
117 ## |
25764
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
118 ## @seealso{textscan, load, dlmread, fscanf, strread} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
119 ## @end deftypefn |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
120 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
121 function varargout = textread (filename, format = "%f", varargin) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
122 |
25764
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
123 persistent warned = false; |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
124 if (! warned) |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
125 warned = true; |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
126 warning ("Octave:legacy-function", |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
127 "textread is obsolete; use textscan instead\n"); |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
128 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
129 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
130 ## Check input |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
131 if (nargin < 1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
132 print_usage (); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
133 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
134 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
135 if (! ischar (filename) || ! ischar (format)) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
136 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
|
137 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
138 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
139 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
|
140 nlines = varargin{1}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
141 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
142 nlines = Inf; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
143 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
144 if (nlines < 1) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
145 printf ("textread: N = 0, no data read\n"); |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
146 varargout = cell (1, nargout); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
147 return; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
148 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
149 |
25764
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
150 BUFLENGTH = 4096; # Read buffer to speed up processing @var{n} |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
151 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
152 ## Read file |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
153 fid = fopen (filename, "r"); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
154 if (fid == -1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
155 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
|
156 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
157 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
158 ## Skip header lines if requested |
9828 | 159 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
|
160 if (! isempty (headerlines)) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
161 ## Beware of missing or wrong headerline value |
21568
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21092
diff
changeset
|
162 if (headerlines == numel (varargin) |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
163 || ! isnumeric (varargin{headerlines + 1})) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
164 error ("textread: missing or invalid value for 'headerlines'" ); |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
165 endif |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
166 ## 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
|
167 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
|
168 ## 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 endif |
20534
66adbb6e88ad
textread.m, textscan.m: always remove headerlines args before invoking strread (bug #46080)
Ethan Biery <ebiery@lutron.com>
parents:
20222
diff
changeset
|
174 varargin(headerlines:headerlines+1) = []; |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
175 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
176 st_pos = ftell (fid); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
177 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21634
diff
changeset
|
178 ## Read a first file chunk. Rest follows after endofline processing |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
179 [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
|
180 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
|
181 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
|
182 varargout = cell (1, nargout); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
183 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
184 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
185 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
186 endofline = find (strcmpi (varargin, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
187 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12880
diff
changeset
|
188 ## 'endofline' option set by user. |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
189 if (ischar (varargin{endofline + 1})) |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
190 eol_char = varargin{endofline + 1}; |
20728
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
191 if (strcmp (typeinfo (eol_char), "sq_string")) |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
192 eol_char = do_string_escapes (eol_char); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
193 endif |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
194 if (! any (strcmp (eol_char, {"", "\n", "\r", "\r\n"}))) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
195 error ("textread: invalid EndOfLine character value specified"); |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
196 endif |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
197 else |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
198 error ("textread: character value required for EndOfLine"); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
199 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
200 else |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
201 ## Determine EOL from file. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
202 ## Search for EOL candidates in the first BUFLENGTH chars |
21092
429f83903400
maint: Use "FIXME:" more places in code base.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
203 ## 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
|
204 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
|
205 ## 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
|
206 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
|
207 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
208 ## 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
|
209 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
|
210 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
|
211 ## 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
|
212 else |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
213 eol_char = "\n"; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
214 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
215 ## 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
|
216 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
|
217 endif |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
218 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
219 ## Now that we know what EOL looks like, we can process format_repeat_count. |
18857
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
220 ## 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
|
221 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
|
222 l_eol_char = length (eol_char); |
25760
2ccad4396afc
findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
223 eoi = strfind (str, eol_char); |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
224 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
225 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
226 ## 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
|
227 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
|
228 [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
|
229 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
230 ## 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
|
231 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
|
232 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
|
233 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
234 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
235 endif |
25760
2ccad4396afc
findstr.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
236 eoi = strfind (str, eol_char); |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
237 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
238 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
239 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
240 endwhile |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
241 ## 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:
20204
diff
changeset
|
242 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:
20204
diff
changeset
|
243 eoi = [ eoi (length (str)) ]; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
244 ++n_eoi; |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
245 endif |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21634
diff
changeset
|
246 ## Found EOL delimiting last requested line. Compute ptr (incl. EOL) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
247 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
248 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
|
249 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
250 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
|
251 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
252 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
|
253 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
|
254 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
255 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
|
256 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
|
257 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
258 fclose (fid); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
259 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
260 ## 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
|
261 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
|
262 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
|
263 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
264 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
265 ## Call strread to make it do the real work |
25763
8dc8edbffa17
strread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25760
diff
changeset
|
266 warning ("off", "Octave:legacy-function", "local"); |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
267 [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
|
268 |
31253
a40c0b7aa376
maint: changes to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
269 ## Hack to concatenate/reshape numeric output into 2-D array (undocumented ML) |
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
|
270 ## 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
|
271 if (isempty (format)) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
272 ## 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
|
273 ## 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
|
274 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
|
275 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
|
276 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
|
277 ## 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 ## 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
|
284 ## 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
|
285 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
|
286 ## 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
|
287 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
|
288 ## 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
|
289 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
|
290 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
|
291 ## 3B. Just count delimiters. FIXME: delimiters could occur in literals |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
292 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
|
293 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
|
294 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
|
295 ## 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 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
|
301 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
|
302 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
|
303 ## 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
|
304 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
|
305 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
|
306 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
307 endfunction |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
308 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
309 |
25764
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
310 ## First test is necessary to provoke 1-time legacy warning |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
311 %!test |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
312 %! warning ("off", "Octave:legacy-function", "local"); |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
313 %! try |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
314 %! textread (""); |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
315 %! catch |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
316 %! ## Nothing to do, just wanted to suppress error. |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
317 %! end_try_catch |
826b45c1c427
textread.m: Make m-file a legacy function.
Rik <rik@octave.org>
parents:
25763
diff
changeset
|
318 |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
319 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
320 %! f = tempname (); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
321 %! 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
|
322 %! dlmwrite (f, d, "precision", "%5.2f"); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
323 %! [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
|
324 %! unlink (f); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
325 %! assert (a, d(4:5, 1), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
326 %! assert (b, d(4:5, 2), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
327 %! assert (c, d(4:5, 3), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
328 |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
329 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
330 %! f = tempname (); |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
331 %! d = rand (7, 2); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
332 %! dlmwrite (f, d, "precision", "%5.2f"); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
333 %! [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
|
334 %! unlink (f); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
335 %! 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
|
336 |
31253
a40c0b7aa376
maint: changes to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
337 ## Test reading 2-D 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
|
338 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
339 %! 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
|
340 %! 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
|
341 %! 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
|
342 %! 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
|
343 %! 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
|
344 %! 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
|
345 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
346 ## 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
|
347 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
348 %! f = tempname (); |
28098
501553fcf6e2
unlink: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
349 %! sts = unlink (f); |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
350 %! 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
|
351 %! 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
|
352 %! 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
|
353 %! 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
|
354 %! 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
|
355 %! 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
|
356 %! 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
|
357 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
358 ## 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
|
359 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
360 %! f = tempname (); |
28098
501553fcf6e2
unlink: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
361 %! sts = unlink (f); |
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
|
362 %! 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
|
363 %! 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
|
364 %! 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
|
365 %! 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
|
366 %! 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
|
367 %! 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
|
368 %! 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
|
369 |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
370 ## 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:
20204
diff
changeset
|
371 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
372 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
373 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
374 %! 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:
20204
diff
changeset
|
375 %! 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:
20204
diff
changeset
|
376 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
377 %! [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:
20204
diff
changeset
|
378 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
379 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
380 %! [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:
20204
diff
changeset
|
381 %! 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:
20204
diff
changeset
|
382 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
383 %! [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:
20204
diff
changeset
|
384 %! 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:
20204
diff
changeset
|
385 %! 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:
20204
diff
changeset
|
386 %! [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:
20204
diff
changeset
|
387 %! 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:
20204
diff
changeset
|
388 %! 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:
20204
diff
changeset
|
389 %! [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:
20204
diff
changeset
|
390 %! 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:
20204
diff
changeset
|
391 %! 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:
20204
diff
changeset
|
392 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
393 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
394 ## 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:
20204
diff
changeset
|
395 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
396 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
397 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
398 %! 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:
20204
diff
changeset
|
399 %! 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:
20204
diff
changeset
|
400 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
401 %! [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:
20204
diff
changeset
|
402 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
403 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
404 %! [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:
20204
diff
changeset
|
405 %! 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:
20204
diff
changeset
|
406 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
407 %! [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:
20204
diff
changeset
|
408 %! 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:
20204
diff
changeset
|
409 %! 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:
20204
diff
changeset
|
410 %! [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:
20204
diff
changeset
|
411 %! 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:
20204
diff
changeset
|
412 %! 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:
20204
diff
changeset
|
413 %! [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:
20204
diff
changeset
|
414 %! 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:
20204
diff
changeset
|
415 %! 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:
20204
diff
changeset
|
416 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
417 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
418 ## 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:
20204
diff
changeset
|
419 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
420 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
421 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
422 %! 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:
20204
diff
changeset
|
423 %! 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:
20204
diff
changeset
|
424 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
425 %! [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:
20204
diff
changeset
|
426 %! assert (a, int32 (10)); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
427 %! assert (b, {"a"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
428 %! [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:
20204
diff
changeset
|
429 %! 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:
20204
diff
changeset
|
430 %! assert (b, {"a"; "b"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
431 %! [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:
20204
diff
changeset
|
432 %! 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:
20204
diff
changeset
|
433 %! 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:
20204
diff
changeset
|
434 %! [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:
20204
diff
changeset
|
435 %! 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:
20204
diff
changeset
|
436 %! 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:
20204
diff
changeset
|
437 %! [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:
20204
diff
changeset
|
438 %! 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:
20204
diff
changeset
|
439 %! 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:
20204
diff
changeset
|
440 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
441 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
442 ## 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:
20204
diff
changeset
|
443 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
444 %! f = tempname (); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
445 %! fid = fopen (f, "w"); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
446 %! 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:
20204
diff
changeset
|
447 %! 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:
20204
diff
changeset
|
448 %! fclose (fid); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
449 %! [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:
20204
diff
changeset
|
450 %! 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:
20204
diff
changeset
|
451 %! assert (b, {"a"; "b"; "c"; ""}); |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
452 %! [a, b] = textread (f, "%d %s", 5); |
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
453 %! assert (a, int32 ([10; 20; 30; 40])); |
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
454 %! assert (b, {"a"; "b"; "c"; ""}); |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
455 %! unlink (f); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
456 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
457 ### Tests with format repeat count #5, nr of data lines = limiting factor |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
458 %!#test |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
459 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
460 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
461 %!# fprintf (fid, "%2d\n%s\n%2dn%s", ... |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
462 %!# 1, "a", 2, "b"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
463 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
464 %!# [a, b] = textread (f, "%d %s", 2); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
465 %!# assert (a, int32 (1)); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
466 %!# assert (b, {"a"}); |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
467 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
468 ### Read multiple lines using empty format string, missing data (should be 0) |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
469 %!#test |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
470 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
471 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
472 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
473 %!# d = rand (1, 4); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
474 %!# fprintf (fid, "%f, %f, , %f, %f ", d); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
475 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
476 %!# A = textread (f, ""); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
477 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
478 %!# assert (A, [ d(1:2) 0 d(3:4)], 1e-6); |
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
|
479 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
480 ### Test with empty positions - ML returns 0 for empty fields |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
481 %!#test |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
482 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
483 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
484 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
485 %!# d = rand (1, 4); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
486 %!# fprintf (fid, ",2,,4\n5,,7,\n"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
487 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
488 %!# A = textread (f, "", "delimiter", ","); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
489 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
490 %!# assert (A, [0 2 0 4; 5 0 7 0], 1e-6); |
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
|
491 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
492 ### Another test with empty format + positions, now with more incomplete lower |
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
493 ### row (must be appended with zeros to get rectangular matrix) |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
494 %!#test |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
495 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
496 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
497 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
498 %!# d = rand (1, 4); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
499 %!# fprintf (fid, ",2,,4\n5,\n"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
500 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
501 %!# A = textread (f, "", "delimiter", ","); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
502 %!# unlink (f); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
503 %!# assert (A, [0 2 0 4; 5 0 0 0], 1e-6); |
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
|
504 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
505 ### Test endofline |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
506 %!#test <*45046> |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
507 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
508 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
509 %!# fprintf (fid, "a\rb\rc"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
510 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
511 %!# ## Test EOL detection |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
512 %!# d = textread (f, "%s"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
513 %!# assert (d, {"a";"b";"c"}); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
514 %!# ## Test explicit EOL specification (bug #45046) |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
515 %!# d = textread (f, "%s", "endofline", "\r"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
516 %!# assert (d, {"a"; "b"; "c"}); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
517 %!# unlink (f); |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
518 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22365
diff
changeset
|
519 ### Properly process single-quoted EOL args |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
520 %!#test <*46477> |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
521 %!# f = tempname (); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
522 %!# fid = fopen (f, "w"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
523 %!# fprintf (fid, "hello, world!"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
524 %!# fclose (fid); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
525 %!# [a, b] = textread (f, "%s%s", "endofline", '\n'); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
526 %!# assert (a{1}, "hello,"); |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
527 %!# assert (b{1}, "world!"); |
20728
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
528 |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
529 ### Test input validation |
31254
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
530 %!#error textread () |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
531 %!#error textread (1) |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
532 %!#error <arguments must be strings> textread (1, "%f") |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
533 %!#error <arguments must be strings> textread ("fname", 1) |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
534 %!#error <missing or invalid value for> textread (file_in_loadpath ("textread.m"), "", "headerlines") |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
535 %!#error <missing or invalid value for> textread (file_in_loadpath ("textread.m"), "", "headerlines", 'hh') |
5d6b058a22dc
maint: use sequence "%!#" to disable BIST tests.
Rik <rik@octave.org>
parents:
31253
diff
changeset
|
536 %!#error <character value required for> textread (file_in_loadpath ("textread.m"), "%s", "endofline", true) |