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