Mercurial > hg > octave-nkf
annotate scripts/io/textread.m @ 19669:c2031ad6dbe7
Fix octave header includes in audiodevinfo
* audiodevinfo.cc: change includes to use local octave headers
author | Vytautas Jančauskas <unaudio@gmail.com> |
---|---|
date | Wed, 11 Sep 2013 21:32:14 +0300 |
parents | 1c89599167a6 |
children | 7bbe3658c5ef 446c46af4b42 |
rev | line source |
---|---|
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1 ## Copyright (C) 2009-2013 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"}: |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
39 ## Specify a single character or @qcode{"\r\n"}. If no value is given, it |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
40 ## will be inferred from the file. If set to "" (empty string) EOLs are |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16816
diff
changeset
|
41 ## ignored as delimiters. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
42 ## @end itemize |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
43 ## |
14621
1804d5422f61
doc: Periodic spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14565
diff
changeset
|
44 ## The optional input @var{n} specifies the number of data lines to read; in |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
45 ## this sense it differs slightly from the format repeat count in strread. |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
46 ## |
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
|
47 ## If the format string is empty (not: omitted) and the file contains only |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
48 ## 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
|
49 ## 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
|
50 ## 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
|
51 ## |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
52 ## @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
|
53 ## @end deftypefn |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
54 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11104
diff
changeset
|
55 function varargout = textread (filename, format = "%f", varargin) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
56 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
57 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
|
58 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
59 ## Check input |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
60 if (nargin < 1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
61 print_usage (); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
62 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
63 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
64 if (! ischar (filename) || ! ischar (format)) |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
65 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
|
66 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
67 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
68 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
|
69 nlines = varargin{1}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
70 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
71 nlines = Inf; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
72 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
73 if (nlines < 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
74 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
|
75 varargout = cell (1, nargout); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
76 return; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
77 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
78 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
79 ## Read file |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
80 fid = fopen (filename, "r"); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
81 if (fid == -1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
82 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
|
83 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
84 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
85 ## Skip header lines if requested |
9828 | 86 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
|
87 if (! isempty (headerlines)) |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
88 ## 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
|
89 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
|
90 || ! 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
|
91 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
|
92 endif |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
93 ## 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
|
94 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
|
95 ## 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
|
96 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
|
97 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
|
98 varargin(headerlines:headerlines+1) = []; |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
99 nargin = nargin - 2; |
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
100 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
|
101 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
|
102 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
103 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
104 st_pos = ftell (fid); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
105 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
106 ## 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
|
107 [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
|
108 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
|
109 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
|
110 varargout = cell (1, nargout); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
111 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
112 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
113 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
114 endofline = find (strcmpi (varargin, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
115 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12880
diff
changeset
|
116 ## 'endofline' option set by user. |
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12880
diff
changeset
|
117 if (! ischar (varargin{endofline + 1})); |
16059
2175c41b12d1
textread.m, textscan.m: catch wrong headerlines values, tests added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
118 error ("character value required for EndOfLine"); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
119 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
120 else |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
121 ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
122 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
|
123 ## 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
|
124 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
|
125 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
126 ## 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
|
127 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
|
128 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
|
129 ## 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
|
130 else |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
131 eol_char = "\n"; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
132 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
133 ## 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
|
134 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
|
135 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
136 |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
137 ## Now that we know what EOL looks like, we can process format_repeat_count. |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
138 ## FIXME The below isn't ML-compatible: counts lines, not format string uses |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
143 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
144 ## 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
|
145 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
|
146 [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
|
147 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
148 ## 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
|
149 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
|
150 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
|
151 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
152 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
153 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
154 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
|
155 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
156 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
157 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
158 endwhile |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
159 ## 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
|
160 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
161 printf ("textread: format repeat count specified but no endofline found\n"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
162 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
|
163 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
164 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
|
165 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
166 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
|
167 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
|
168 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
169 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
|
170 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
|
171 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
172 fclose (fid); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14430
diff
changeset
|
173 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
174 ## 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
|
175 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
|
176 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
|
177 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12860
diff
changeset
|
178 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
179 ## Call strread to make it do the real work |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
180 [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
|
181 |
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
|
182 ## 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
|
183 ## 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
|
184 if (isempty (format)) |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
185 ## Get number of 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
|
186 ## 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
|
187 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
|
188 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
|
189 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
|
190 ## 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 ## 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
|
197 ## 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
|
198 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
|
199 ## 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
|
200 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
|
201 ## 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
|
202 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
|
203 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
|
204 ## 3B. Just count delimiters. FIXME: delimiters could occur in literals |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
205 delimiter = varargin {idelimiter+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
|
206 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
|
207 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
|
208 ## 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 ## 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
|
217 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
|
218 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
|
219 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
220 endfunction |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
221 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17312
diff
changeset
|
222 |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
223 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
224 %! f = tmpnam (); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
225 %! 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
|
226 %! dlmwrite (f, d, "precision", "%5.2f"); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
227 %! [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
|
228 %! unlink (f); |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
229 %! assert (a, d(4:5, 1), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
230 %! assert (b, d(4:5, 2), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
231 %! assert (c, d(4:5, 3), 1e-2); |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
232 |
14430
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
233 %!test |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
234 %! f = tmpnam (); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
235 %! d = rand (7, 2); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
236 %! dlmwrite (f, d, "precision", "%5.2f"); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
237 %! [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
|
238 %! unlink (f); |
df5488e46dca
fix bug in input validation for textread
Carlo de Falco <kingcrimson@tiscali.it>
parents:
14363
diff
changeset
|
239 %! 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
|
240 |
16357
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
241 %% Test reading 2D matrix with empty format |
0cbe330f39a2
textscan.m, 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 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
243 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
244 %! 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
|
245 %! 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
|
246 %! 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
|
247 %! 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
|
248 %! 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
|
249 |
0cbe330f39a2
textscan.m, 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 %% Read multiple lines using 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
|
251 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
252 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
253 %! 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
|
254 %! 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
|
255 %! 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
|
256 %! 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
|
257 %! 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
|
258 %! 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
|
259 %! 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
|
260 %! 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
|
261 |
0cbe330f39a2
textscan.m, 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 %% Empty format, corner case = one line w/o EOL |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
263 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
264 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
265 %! 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
|
266 %! 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
|
267 %! 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
|
268 %! 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
|
269 %! 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
|
270 %! 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
|
271 %! 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
|
272 %! 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
|
273 |
0cbe330f39a2
textscan.m, 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 %% Read multiple lines using empty format string, missing data (should be 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
|
275 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
276 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
277 %! 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
|
278 %! 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
|
279 %! 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
|
280 %! 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
|
281 %! 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
|
282 %! 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
|
283 %! 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
|
284 %! 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
|
285 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
286 %% Test with empty positions - ML returns 0 for empty fields |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
287 %!test |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
288 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
289 %! 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
|
290 %! 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
|
291 %! 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
|
292 %! 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
|
293 %! 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
|
294 %! 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
|
295 %! 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
|
296 %! 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
|
297 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
298 %% Another test with empty format + positions, now with more incomplete lower |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
299 %% row (must be appended with zeros to get rectangular matrix) |
0cbe330f39a2
textscan.m, 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 |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
301 %! f = tmpnam (); |
0cbe330f39a2
textscan.m, textread.m: allow reading multi-column data files with empty format + tests (bug #38317)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16311
diff
changeset
|
302 %! 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
|
303 %! 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
|
304 %! 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
|
305 %! 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
|
306 %! 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
|
307 %! 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
|
308 %! 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
|
309 %! 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
|
310 |
12860
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
311 %% Test input validation |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
312 %!error textread () |
abd4093753e0
textread.m: Tests for function
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
313 %!error textread (1) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
314 %!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
|
315 %!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
|
316 %!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
|
317 %!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
|
318 %!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
|
319 |