Mercurial > octave
annotate scripts/io/textread.m @ 21633:dcf8922b724b
Deprecate printf, puts, and scanf. Make fputs a M-file.
* libinterp/corefcn/file-io.cc: extracted printf, puts, and scanf to M-files.
* scripts/deprecated/printf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/puts.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/scanf.m: new function, equivalent to version file-io.cc.
* scripts/deprecated/module.mk: added printf, puts, and scanf.
* scripts/io/fputs.m: new function, equivalent to version file-io.cc.
* scripts/io/module.mk: added fputs.
* doc/interpreter/io.txi: adapted documentation to current implementation.
* NEWS: announced changes.
* doc/interpreter/basics.txi: replaced printf by fprintf.
* doc/interpreter/stmt.txi: replaced printf by fprintf.
* doc/interpreter/var.txi: replaced printf by fprintf.
* doc/refcard/refcard.tex: removed printf and scanf.
* examples/code/@FIRfilter/display.m: replaced printf by fprintf.
* examples/code/@polynomial/display.m: replaced printf by fprintf.
* scripts/@ftp/display.m: replaced printf by fprintf.
* scripts/general/inputParser.m: replaced printf by fprintf.
* scripts/general/methods.m: replaced printf by fprintf.
* scripts/general/profexplore.m: replaced printf by fprintf.
* scripts/general/profshow.m: replaced printf by fprintf.
* scripts/help/help.m: replaced puts by fputs and printf by fprintf.
* scripts/help/lookfor.m: replaced puts by fputs and printf by fprintf.
* scripts/help/which.m: replaced printf by fprintf.
* scripts/image/imformats.m: replaced printf by fprintf.
* scripts/io/beep.m: replaced puts by fputs.
* scripts/io/textread.m: replaced printf by fprintf.
* scripts/java/javaclasspath.m: replaced printf by fprintf.
* scripts/java/javamem.m: replaced printf by fprintf.
* scripts/miscellaneous/dir.m: replaced printf by fprintf.
* scripts/miscellaneous/dos.m: replaced printf by fprintf.
* scripts/miscellaneous/fact.m: replaced printf by fprintf.
* scripts/miscellaneous/info.m: replaced printf by fprintf.
* scripts/miscellaneous/license.m: replaced printf by fprintf.
* scripts/miscellaneous/ls.m: replaced puts by fputs.
* scripts/miscellaneous/menu.m: replaced printf by fprintf.
* scripts/miscellaneous/mkoctfile.m: replaced printf by fprintf.
* scripts/miscellaneous/private/display_info_file.m:
* scripts/miscellaneous/unix.m: replaced printf by fprintf.
* scripts/miscellaneous/ver.m: replaced printf by fprintf.
* scripts/miscellaneous/what.m: replaced printf by fprintf.
* scripts/ode/ode23.m: replaced printf by fprintf.
* scripts/ode/ode45.m: replaced printf by fprintf.
* scripts/optimization/fminbnd.m: replaced printf by fprintf.
* scripts/optimization/optimset.m: replaced puts by fputs and printf by fprintf.
* scripts/optimization/sqp.m: replaced printf by fprintf.
* scripts/pkg/pkg.m: replaced printf by fprintf.
* scripts/pkg/private/describe.m: replaced printf by fprintf.
* scripts/pkg/private/install.m: replaced printf by fprintf.
* scripts/pkg/private/installed_packages.m: replaced printf by fprintf.
* scripts/pkg/private/list_forge_packages.m: replaced puts by fputs and printf by fprintf.
* scripts/pkg/private/rebuild.m: replaced printf by fprintf.
* scripts/plot/util/__gnuplot_drawnow__.m: replaced puts by fputs and printf by fprintf.
* scripts/signal/stft.m: replaced printf by fprintf.
* scripts/sparse/bicg.m: replaced printf by fprintf.
* scripts/sparse/bicgstab.m: replaced printf by fprintf.
* scripts/sparse/cgs.m: replaced printf by fprintf.
* scripts/sparse/pcg.m: replaced printf by fprintf.
* scripts/sparse/pcr.m: replaced printf by fprintf.
* scripts/sparse/qmr.m: replaced printf by fprintf.
* scripts/statistics/models/logistic_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/anova.m: replaced printf by fprintf.
* scripts/statistics/tests/bartlett_test.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_homogeneity.m: replaced printf by fprintf.
* scripts/statistics/tests/chisquare_test_independence.m: replaced printf by fprintf.
* scripts/statistics/tests/cor_test.m: replaced printf by fprintf.
* scripts/statistics/tests/f_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test.m: replaced printf by fprintf.
* scripts/statistics/tests/hotelling_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test.m: replaced printf by fprintf.
* scripts/statistics/tests/kolmogorov_smirnov_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/kruskal_wallis_test.m: replaced printf by fprintf.
* scripts/statistics/tests/manova.m: replaced printf by fprintf.
* scripts/statistics/tests/mcnemar_test.m: replaced printf by fprintf.
* scripts/statistics/tests/prop_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/run_test.m: replaced printf by fprintf.
* scripts/statistics/tests/sign_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_2.m: replaced printf by fprintf.
* scripts/statistics/tests/t_test_regression.m: replaced printf by fprintf.
* scripts/statistics/tests/u_test.m: replaced printf by fprintf.
* scripts/statistics/tests/var_test.m: replaced printf by fprintf.
* scripts/statistics/tests/welch_test.m: replaced printf by fprintf.
* scripts/statistics/tests/wilcoxon_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test.m: replaced printf by fprintf.
* scripts/statistics/tests/z_test_2.m: replaced printf by fprintf.
* scripts/strings/strtok.m: replaced printf by fprintf.
* scripts/testfun/__run_test_suite__.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/demo.m: replaced printf by fprintf.
* scripts/testfun/example.m: replaced printf by fprintf.
* scripts/testfun/private/compare_plot_demos.m: replaced printf by fprintf.
* scripts/testfun/rundemos.m: replaced printf by fprintf.
* scripts/testfun/runtests.m: replaced puts by fputs and printf by fprintf.
* scripts/testfun/speed.m: replaced printf by fprintf.
* scripts/testfun/test.m: replaced printf by fprintf.
* scripts/time/calendar.m: replaced puts by fputs and printf by fprintf.
* test/io.tst: replaced printf by fprintf.
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Wed, 20 Apr 2016 17:53:10 +0200 |
parents | 3d60ed163b70 |
children | 96518f623c91 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
1 ## Copyright (C) 2009-2015 Eric Chassande-Mottin, CNRS (France) |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
2 ## |
11104 | 3 ## This file is part of Octave. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
4 ## |
11104 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
14 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
11104 | 16 ## along with Octave; see the file COPYING. If not, see |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
18 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20742
diff
changeset
|
20 ## @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
|
21 ## @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
|
22 ## @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
|
23 ## @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
|
24 ## @deftypefnx {} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @var{n}, @var{prop1}, @var{value1}, @dots{}) |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
25 ## Read data from a text file. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
26 ## |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9746
diff
changeset
|
27 ## The file @var{filename} is read and parsed according to @var{format}. The |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10627
diff
changeset
|
28 ## function behaves like @code{strread} except it works by parsing a file |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
29 ## instead of a string. See the documentation of @code{strread} for details. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
30 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
31 ## In addition to the options supported by @code{strread}, this function |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
32 ## supports two more: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
33 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
34 ## @itemize |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
35 ## @item @qcode{"headerlines"}: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
36 ## The first @var{value} number of lines of @var{filename} are skipped. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
37 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
38 ## @item @qcode{"endofline"}: |
20101
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
39 ## Specify a single character or |
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
40 ## @qcode{"@xbackslashchar{}r@xbackslashchar{}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
|
41 ## will be inferred from the file. If set to @qcode{""} (empty string) EOLs |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20101
diff
changeset
|
42 ## are ignored as delimiters. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
43 ## @end itemize |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
44 ## |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
45 ## The optional input @var{n} (format repeat count) specifies the number of |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
46 ## times the format string is to be used or the number of lines to be read, |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
47 ## whichever happens first while reading. The former is equivalent to |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
48 ## requesting that the data output vectors should be of length @var{N}. |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
49 ## Note that when reading files with format strings referring to multiple |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
50 ## lines, @var{n} should rather be the number of lines to be read than the |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
51 ## number of format string uses. |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
52 ## |
20160
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20101
diff
changeset
|
53 ## If the format string is empty (not just omitted) and the file contains only |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
54 ## numeric data (excluding headerlines), textread will return a rectangular |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
55 ## matrix with the number of columns matching the number of numeric fields on |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16357
diff
changeset
|
56 ## the first data line of the file. Empty fields are returned as zero values. |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
57 ## |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
58 ## Examples: |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
59 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
60 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
61 ## @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
|
62 ## 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
|
63 ## 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
|
64 ## 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
|
65 ## 5 e |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
66 ## @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
|
67 ## @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
|
68 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
69 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
70 ## @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
|
71 ## [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
|
72 ## 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
|
73 ## cellstr array: |
20222
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
74 ## 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
|
75 ## b = @{"a"; "b"; "c"; "d"; "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", 3) |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
82 ## (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
|
83 ## 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
|
84 ## returns |
20222
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
85 ## a = [1; 2; 3] |
8a25649b9c77
textread.m: fix texinfo header syntax after commit 481fa65e5cab
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20220
diff
changeset
|
86 ## 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
|
87 ## |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
88 ## @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
|
89 ## @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
|
90 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
91 ## @example |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
92 ## @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
|
93 ## 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
|
94 ## 1 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
95 ## a |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
96 ## 2 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
97 ## b |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
98 ## |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
99 ## [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
|
100 ## 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
|
101 ## 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
|
102 ## 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
|
103 ## (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
|
104 ## than 2. |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20591
diff
changeset
|
105 ## @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
|
106 ## @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
|
107 ## |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
108 ## @seealso{strread, load, dlmread, fscanf, textscan} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
109 ## @end deftypefn |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
110 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
111 function varargout = textread (filename, format = "%f", varargin) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
112 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
113 BUFLENGTH = 4096; # Read buffer to speed up processing @var{n} |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
114 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
115 ## Check input |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
116 if (nargin < 1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
117 print_usage (); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
118 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
119 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
120 if (! ischar (filename) || ! ischar (format)) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
121 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
|
122 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
123 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
124 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
|
125 nlines = varargin{1}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
126 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
127 nlines = Inf; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
128 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
129 if (nlines < 1) |
21633
dcf8922b724b
Deprecate printf, puts, and scanf. Make fputs a M-file.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21568
diff
changeset
|
130 fprintf ("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
|
131 varargout = cell (1, nargout); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
132 return; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
133 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
134 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
135 ## Read file |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
136 fid = fopen (filename, "r"); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
137 if (fid == -1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
138 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
|
139 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
140 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
141 ## Skip header lines if requested |
9828 | 142 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
|
143 if (! isempty (headerlines)) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
144 ## Beware of missing or wrong headerline value |
21568
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
21092
diff
changeset
|
145 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
|
146 || ! isnumeric (varargin{headerlines + 1})) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
147 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
|
148 endif |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
149 ## 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
|
150 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
|
151 ## 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 varargin(headerlines:headerlines+1) = []; |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
158 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
159 st_pos = ftell (fid); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
160 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
161 ## Read a first file chunk. Rest follows after endofline processing |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
162 [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
|
163 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
|
164 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
|
165 varargout = cell (1, nargout); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
166 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
167 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
168 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
169 endofline = find (strcmpi (varargin, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
170 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12880
diff
changeset
|
171 ## '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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 endif |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
177 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
|
178 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
|
179 endif |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
180 else |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
181 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
|
182 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
183 else |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
184 ## Determine EOL from file. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
185 ## 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
|
186 ## 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
|
187 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
|
188 ## 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
|
189 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
|
190 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
191 ## 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
|
192 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
|
193 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
|
194 ## 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
|
195 else |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
196 eol_char = "\n"; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
197 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
198 ## 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
|
199 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
|
200 endif |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
201 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
202 ## 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
|
203 ## 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
|
204 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
|
205 l_eol_char = length (eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
206 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
207 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
208 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
209 ## 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
|
210 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
|
211 [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
|
212 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
213 ## 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
|
214 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
|
215 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
|
216 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
217 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
218 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
219 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
220 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
221 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
222 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
223 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
|
224 ## 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
|
225 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
|
226 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
|
227 ++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
|
228 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
229 ## Found EOL delimiting last requested line. Compute ptr (incl. EOL) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
230 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
231 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
|
232 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
233 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
|
234 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
235 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
|
236 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
|
237 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
238 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
|
239 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
|
240 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
241 fclose (fid); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
242 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
243 ## 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
|
244 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
|
245 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
|
246 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
247 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
248 ## Call strread to make it do the real work |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
249 [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
|
250 |
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
|
251 ## Hack to concatenate/reshape numeric output into 2D array (undocumented ML) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
252 ## 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
|
253 if (isempty (format)) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17338
diff
changeset
|
254 ## 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
|
255 ## 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
|
256 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
|
257 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
|
258 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
|
259 ## 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 ## 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
|
266 ## 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
|
267 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
|
268 ## 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
|
269 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
|
270 ## 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
|
271 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
|
272 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
|
273 ## 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
|
274 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
|
275 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
|
276 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
|
277 ## 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 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
|
285 ## 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
|
286 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
|
287 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
|
288 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
289 endfunction |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
290 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
291 |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
292 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
293 %! f = tempname (); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
294 %! 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
|
295 %! dlmwrite (f, d, "precision", "%5.2f"); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
296 %! [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
|
297 %! unlink (f); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
298 %! assert (a, d(4:5, 1), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
299 %! assert (b, d(4:5, 2), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
300 %! assert (c, d(4:5, 3), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
301 |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
302 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
303 %! f = tempname (); |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
304 %! d = rand (7, 2); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
305 %! dlmwrite (f, d, "precision", "%5.2f"); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
306 %! [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
|
307 %! unlink (f); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
308 %! 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
|
309 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
310 ## Test reading 2D matrix with empty format |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
311 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
312 %! 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
|
313 %! 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
|
314 %! 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
|
315 %! 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
|
316 %! 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
|
317 %! 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
|
318 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
319 ## 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
|
320 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
321 %! 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
|
322 %! 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
|
323 %! 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
|
324 %! 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
|
325 %! 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
|
326 %! 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
|
327 %! 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
|
328 %! 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
|
329 %! 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
|
330 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
331 ## 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
|
332 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
333 %! 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
|
334 %! 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
|
335 %! 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
|
336 %! 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
|
337 %! 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
|
338 %! 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
|
339 %! 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
|
340 %! 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
|
341 %! 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
|
342 |
20220
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
343 ## 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
|
344 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
345 %! 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
|
346 %! 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
|
347 %! 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
|
348 %! 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
|
349 %! 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
|
350 %! [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
|
351 %! 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
|
352 %! 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
|
353 %! [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
|
354 %! 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
|
355 %! 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
|
356 %! [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
|
357 %! 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
|
358 %! 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
|
359 %! [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
|
360 %! 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
|
361 %! 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
|
362 %! [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
|
363 %! 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
|
364 %! 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
|
365 %! 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
|
366 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
367 ## 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
|
368 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
369 %! 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
|
370 %! 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
|
371 %! 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
|
372 %! 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
|
373 %! 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
|
374 %! [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
|
375 %! 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
|
376 %! 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
|
377 %! [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
|
378 %! 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
|
379 %! 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
|
380 %! [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
|
381 %! 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
|
382 %! 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
|
383 %! [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
|
384 %! 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
|
385 %! 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
|
386 %! [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
|
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 %! 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
|
390 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
391 ## 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
|
392 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
393 %! 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
|
394 %! 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
|
395 %! 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
|
396 %! 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
|
397 %! 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
|
398 %! [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
|
399 %! 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
|
400 %! 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
|
401 %! [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
|
402 %! 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
|
403 %! 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
|
404 %! [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
|
405 %! 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
|
406 %! 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
|
407 %! [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
|
408 %! 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
|
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", 5); |
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"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
413 %! 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
|
414 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
415 ## 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
|
416 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
417 %! 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
|
418 %! 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
|
419 %! 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
|
420 %! 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
|
421 %! 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
|
422 %! [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
|
423 %! 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
|
424 %! 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
|
425 #%! [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
|
426 #%! 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
|
427 #%! 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
|
428 %! 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
|
429 |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
430 ## Tests with format repeat count #5, nr of data lines = limiting factor |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
431 %!test |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
432 %! 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
|
433 %! 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
|
434 %! fprintf (fid, "%2d\n%s\n%2dn%s", ... |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
435 %! 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
|
436 %! 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
|
437 %! [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
|
438 %! assert (a, int32 (1)); |
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"}); |
481fa65e5cab
textread.m, textscan.m: Fix handling of format repeat count (bug #45047)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20204
diff
changeset
|
440 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
441 ## Read multiple lines using empty format string, missing data (should be 0) |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
442 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
443 %! 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
|
444 %! 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
|
445 %! 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
|
446 %! 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
|
447 %! 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
|
448 %! 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
|
449 %! 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
|
450 %! 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
|
451 %! assert (A, [ d(1:2) 0 d(3:4)], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
452 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
453 ## Test with empty positions - ML returns 0 for empty fields |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
454 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
455 %! 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
|
456 %! 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
|
457 %! 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
|
458 %! 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
|
459 %! fprintf (fid, ",2,,4\n5,,7,\n"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
460 %! 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
|
461 %! A = textread (f, "", "delimiter", ","); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
462 %! 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
|
463 %! assert (A, [0 2 0 4; 5 0 7 0], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
464 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
465 ## Another test with empty format + positions, now with more incomplete lower |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
466 ## row (must be appended with zeros to get rectangular matrix) |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
467 %!test |
19278
6ca096827123
Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents:
18857
diff
changeset
|
468 %! 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
|
469 %! 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
|
470 %! 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
|
471 %! 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
|
472 %! fprintf (fid, ",2,,4\n5,\n"); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
473 %! 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
|
474 %! A = textread (f, "", "delimiter", ","); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
475 %! 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
|
476 %! assert (A, [0 2 0 4; 5 0 0 0], 1e-6); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
477 |
20204
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
478 ## Test endofline |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
479 %!test |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
480 %! f = tempname (); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
481 %! fid = fopen (f, "w"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
482 %! fprintf (fid, "a\rb\rc"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
483 %! fclose (fid); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
484 %! ## Test EOL detection |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
485 %! d = textread (f, "%s"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
486 %! assert (d, {"a";"b";"c"}); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
487 %! ## Test explicit EOL specification (bug #45046) |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
488 %! d = textread (f, "%s", "endofline", "\r"); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
489 %! assert (d, {"a"; "b"; "c"}); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
490 %! unlink (f); |
935832827f47
textread.m: properly initialize endofline if specified by user (bug #45046)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20160
diff
changeset
|
491 |
20728
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
492 ## Properly process single-quoted EOL args (bug #46477) |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
493 %!test |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
494 %! f = tempname (); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
495 %! fid = fopen (f, "w"); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
496 %! fprintf (fid, "hello, world!"); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
497 %! fclose (fid); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
498 %! [a, b] = textread (f, "%s%s", "endofline", '\n'); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
499 %! assert (a{1}, "hello,"); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
500 %! assert (b{1}, "world!"); |
ab0d9d7f479c
textread.m, textscan.m: properly process single-quoted endofline parameter (bug #46477)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20534
diff
changeset
|
501 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
502 ## Test input validation |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
503 %!error textread () |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
504 %!error textread (1) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
505 %!error <arguments must be strings> textread (1, "%f") |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
506 %!error <arguments must be strings> textread ("fname", 1) |
20726
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
507 %!error <missing or invalid value for> textread (file_in_loadpath ("textread.m"), "", "headerlines") |
25d676f9619c
Preface error() messages with name of function when possible.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
508 %!error <missing or invalid value for> textread (file_in_loadpath ("textread.m"), "", "headerlines", 'hh') |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
509 %!error <character value required for> textread (file_in_loadpath ("textread.m"), "%s", "endofline", true) |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
510 |