Mercurial > hg > octave-nkf
annotate scripts/io/strread.m @ 20778:af5591ef9790 stable
__gnuplot_drawnow__.m: Use "screenpixelsperinch" instead of constant value (bug #46122).
author | ederag <edera@gmx.fr> |
---|---|
date | Tue, 06 Oct 2015 11:07:10 +0200 |
parents | d43675752d04 |
children | 642ce72cf1ab |
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) |
20656
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
2 ## Parts Copyright (C) 2012-2015 Philip Nienhuis |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
3 ## |
11104 | 4 ## 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
|
5 ## |
11104 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## 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
|
15 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
16 ## You should have received a copy of the GNU General Public License |
11104 | 17 ## 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
|
18 ## <http://www.gnu.org/licenses/>. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
19 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
20 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {[@var{a}, @dots{}] =} strread (@var{str}) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} strread (@var{str}, @var{format}) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} strread (@var{str}, @var{format}, @var{format_repeat}) |
11150 | 24 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} strread (@var{str}, @var{format}, @var{prop1}, @var{value1}, @dots{}) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} strread (@var{str}, @var{format}, @var{format_repeat}, @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
|
26 ## Read data from a string. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
27 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
28 ## The string @var{str} is split into words that are repeatedly matched to the |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
29 ## specifiers in @var{format}. The first word is matched to the first |
12879
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
30 ## specifier, the second to the second specifier and so forth. If there are |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
31 ## more words than specifiers, the process is repeated until all words have |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
32 ## been processed. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
33 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
34 ## The string @var{format} describes how the words in @var{str} should be |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
35 ## parsed. It may contain any combination of the following specifiers: |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14317
diff
changeset
|
36 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
37 ## @table @code |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
38 ## @item %s |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
39 ## The word is parsed as a string. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 ## |
16094
8899c785cc99
doc: Fix warnings associated with Texinfo 5.0 (bug #38392)
Rik <rik@octave.org>
parents:
15855
diff
changeset
|
41 ## @item %f |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
42 ## @itemx %n |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
43 ## The word is parsed as a number and converted to double. |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
44 ## |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
45 ## @item %d |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
46 ## @itemx %u |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
47 ## The word is parsed as a number and converted to int32. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
48 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
49 ## @item %*', '%*f', '%*s |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
50 ## The word is skipped. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
51 ## |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
52 ## For %s and %d, %f, %n, %u and the associated %*s @dots{} specifiers an |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
53 ## optional width can be specified as %Ns, etc. where N is an integer > 1. |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
54 ## For %f, format specifiers like %N.Mf are allowed. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
55 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
56 ## @item literals |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
57 ## In addition the format may contain literal character strings; these will be |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
58 ## skipped during reading. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
59 ## @end table |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
60 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
61 ## Parsed word corresponding to the first specifier are returned in the first |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
62 ## output argument and likewise for the rest of the specifiers. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
63 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
64 ## By default, @var{format} is @t{"%f"}, meaning that numbers are read from |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
65 ## @var{str}. This will do if @var{str} contains only numeric fields. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
66 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
67 ## For example, the string |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
68 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
69 ## @example |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
70 ## @group |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
71 ## @var{str} = "\ |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
72 ## Bunny Bugs 5.5\n\ |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
73 ## Duck Daffy -7.5e-5\n\ |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
74 ## Penguin Tux 6" |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
75 ## @end group |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
76 ## @end example |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
77 ## |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
78 ## @noindent |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
79 ## can be read using |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
80 ## |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
81 ## @example |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
82 ## [@var{a}, @var{b}, @var{c}] = strread (@var{str}, "%s %s %f"); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
83 ## @end example |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
84 ## |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
85 ## Optional numeric argument @var{format_repeat} can be used for limiting the |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
86 ## number of items read: |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14317
diff
changeset
|
87 ## |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
88 ## @table @asis |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
89 ## @item -1 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
90 ## (default) read all of the string until the end. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
91 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
92 ## @item N |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
93 ## Read N times @var{nargout} items. 0 (zero) is an acceptable value for |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
94 ## @var{format_repeat}. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
95 ## @end table |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
96 ## |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
97 ## The behavior of @code{strread} can be changed via property-value pairs. The |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
98 ## following properties are recognized: |
11147
a81994607ca0
document textscan and textread
John W. Eaton <jwe@octave.org>
parents:
11127
diff
changeset
|
99 ## |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
100 ## @table @asis |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
101 ## @item @qcode{"commentstyle"} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
102 ## Parts of @var{str} are considered comments and will be skipped. |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
103 ## @var{value} is the comment style and can be any of the following. |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
104 ## |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
105 ## @itemize |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
106 ## @item @qcode{"shell"} |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
107 ## Everything from @code{#} characters to the nearest end-of-line is skipped. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
108 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
109 ## @item @qcode{"c"} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
110 ## Everything between @code{/*} and @code{*/} is skipped. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
111 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
112 ## @item @qcode{"c++"} |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
113 ## Everything from @code{//} characters to the nearest end-of-line is skipped. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
114 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
115 ## @item @qcode{"matlab"} |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
116 ## Everything from @code{%} characters to the nearest end-of-line is skipped. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
117 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
118 ## @item user-supplied. Two options: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
119 ## (1) One string, or 1x1 cell string: Skip everything to the right of it; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
120 ## (2) 2x1 cell string array: Everything between the left and right strings |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
121 ## is skipped. |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
122 ## @end itemize |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
123 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
124 ## @item @qcode{"delimiter"} |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
125 ## Any character in @var{value} will be used to split @var{str} into words |
20656
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
126 ## (default value = any whitespace). Note that whitespace is implicitly added |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
127 ## to the set of delimiter characters unless a @qcode{"%s"} format conversion |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
128 ## specifier is supplied; see @qcode{"whitespace"} parameter below. The set |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
129 ## of delimiter characters cannot be empty; if needed Octave substitutes a |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
130 ## space as delimiter. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
131 ## |
20656
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
132 ## @item @qcode{"emptyvalue"} |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
133 ## Value to return for empty numeric values in non-whitespace delimited data. |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
134 ## The default is NaN@. When the data type does not support NaN (int32 for |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
135 ## example), then default is zero. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
136 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
137 ## @item @qcode{"multipledelimsasone"} |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
138 ## Treat a series of consecutive delimiters, without whitespace in between, |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
139 ## as a single delimiter. Consecutive delimiter series need not be vertically |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
140 ## @qcode{"aligned"}. |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
141 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
142 ## @item @qcode{"treatasempty"} |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
143 ## Treat single occurrences (surrounded by delimiters or whitespace) of the |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
144 ## string(s) in @var{value} as missing values. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
145 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
146 ## @item @qcode{"returnonerror"} |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
147 ## If @var{value} true (1, default), ignore read errors and return normally. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
148 ## If false (0), return an error. |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
149 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
150 ## @item @qcode{"whitespace"} |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
151 ## Any character in @var{value} will be interpreted as whitespace and trimmed; |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
152 ## the string defining whitespace must be enclosed in double quotes for proper |
20656
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
153 ## processing of special characters like @qcode{"@xbackslashchar{}t"}. In |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
154 ## each data field, multiple consecutive whitespace characters are collapsed |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
155 ## into one space and leading and trailing whitespace is removed. The default |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
156 ## value for whitespace is |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
157 ## @c Note: the next line specifically has a newline which generates a space |
20391
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20370
diff
changeset
|
158 ## @c in the output of qcode, but keeps the next line < 80 characters. |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
159 ## @qcode{" |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
160 ## @xbackslashchar{}b@xbackslashchar{}r@xbackslashchar{}n@xbackslashchar{}t"} |
20656
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
161 ## (note the space). Whitespace is always added to the set of delimiter |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
162 ## characters unless at least one @qcode{"%s"} format conversion specifier is |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
163 ## supplied; in that case only whitespace explicitly specified in |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
164 ## @qcode{"delimiter"} is retained as delimiter and removed from the set of |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
165 ## whitespace characters. If whitespace characters are to be kept as-is (in |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
166 ## e.g., strings), specify an empty value (i.e., @qcode{""}) for |
d43675752d04
strread.m: clarify delimiter and whitespace usage (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20652
diff
changeset
|
167 ## @qcode{"whitespace"}; obviously, whitespace cannot be a delimiter then. |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
168 ## |
11150 | 169 ## @end table |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
170 ## |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
171 ## When the number of words in @var{str} doesn't match an exact multiple of |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
172 ## the number of format conversion specifiers, strread's behavior depends on |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
173 ## the last character of @var{str}: |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
174 ## |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
175 ## @table @asis |
20311
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20245
diff
changeset
|
176 ## @item last character = @qcode{"@xbackslashchar{}n"} |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
177 ## Data columns are padded with empty fields or Nan so that all columns have |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20311
diff
changeset
|
178 ## equal length |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
179 ## |
20311
e51473fdb622
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20245
diff
changeset
|
180 ## @item last character is not @qcode{"@xbackslashchar{}n"} |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
181 ## Data columns are not padded; strread returns columns of unequal length |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
182 ## |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
183 ## @end table |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14582
diff
changeset
|
184 ## |
14853
72b8b39e12be
doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14835
diff
changeset
|
185 ## @seealso{textscan, textread, load, dlmread, fscanf} |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
186 ## @end deftypefn |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
187 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11343
diff
changeset
|
188 function varargout = strread (str, format = "%f", varargin) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
189 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
190 ## Check input |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
191 if (nargin < 1) |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
192 print_usage (); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
193 endif |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19050
diff
changeset
|
194 |
14497
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
195 if (isempty (str)) |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
196 ## Return empty args (no match), rather than raising an error |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
197 varargout = cell (1, nargout); |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
198 return; |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
199 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
200 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
201 if (isempty (format)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
202 format = "%f"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
203 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
204 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
205 if (! ischar (str) || ! ischar (format)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
206 error ("strread: STR 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
|
207 endif |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
208 |
19050
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
209 if (strcmp (typeinfo (format), "sq_string")) |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
210 format = do_string_escapes (format); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
211 endif |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
212 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
213 ## Parse format string to compare number of conversion fields and nargout |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
214 nfields = numel (regexp (format, '(%(\d*|\d*\.\d*)?[nfduscq]|%\[)', "match")); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
215 ## If str only has numeric fields, a (default) format ("%f") will do. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
216 ## Otherwise: |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
217 if (! nfields) |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
218 error ("strread.m: no valid format conversion specifiers found\n"); |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
219 elseif ((max (nargout, 1) != nfields) && ! strcmp (format, "%f")) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
220 error ("strread: the number of output variables must match that specified by FORMAT"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
221 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
222 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
223 ## Check for format string repeat count |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
224 format_repeat_count = -1; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
225 if (nargin > 2 && isnumeric (varargin{1})) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
226 if (varargin{1} >= 0) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
227 format_repeat_count = varargin{1}; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
228 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
229 if (nargin > 3) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
230 varargin = varargin(2:end); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
231 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
232 varargin = {}; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
233 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
234 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
235 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
236 ## Parse options. First initialize defaults |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
237 comment_flag = false; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
238 delimiter_str = ""; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
239 empty_str = ""; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
240 eol_char = ""; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
241 err_action = 0; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
242 mult_dlms_s1 = false; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
243 numeric_fill_value = NaN; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
244 white_spaces = " \b\r\n\t"; |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
245 for n = 1:2:length (varargin) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
246 switch (lower (varargin{n})) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
247 case "bufsize" |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
248 ## We could synthesize this, but that just seems weird... |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
249 warning ("strread: property 'bufsize' is not implemented"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
250 case "commentstyle" |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
251 comment_flag = true; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
252 switch (lower (varargin{n+1})) |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
253 case "c" |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
254 [comment_start, comment_end] = deal ("/*", "*/"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
255 case "c++" |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
256 [comment_start, comment_end] = deal ("//", "eol_char"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
257 case "shell" |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
258 [comment_start, comment_end] = deal ("#" , "eol_char"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
259 case "matlab" |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
260 [comment_start, comment_end] = deal ("%" , "eol_char"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
261 otherwise |
20072
e9f89866074c
maint: Cleanup some .m files to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
20038
diff
changeset
|
262 if (ischar (varargin{n+1}) |
e9f89866074c
maint: Cleanup some .m files to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
20038
diff
changeset
|
263 || (numel (varargin{n+1}) == 1 && iscellstr (varargin{n+1}))) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
264 [comment_start, comment_end] = deal (char (varargin{n+1}), "eol_char"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
265 elseif (iscellstr (varargin{n+1}) && numel (varargin{n+1}) == 2) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
266 [comment_start, comment_end] = deal (varargin{n+1}{:}); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
267 else |
19047
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
268 ## FIXME: A user may have numeric values specified: {'//', 7} |
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
269 ## this will lead to an error in the warning message |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
270 error ("strread: unknown or unrecognized comment style '%s'", |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
271 varargin{n+1}); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
272 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
273 endswitch |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
274 case "delimiter" |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
275 delimiter_str = varargin{n+1}; |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
276 if (strcmp (typeinfo (delimiter_str), "sq_string")) |
13715
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
277 delimiter_str = do_string_escapes (delimiter_str); |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
278 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
279 case "emptyvalue" |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
280 numeric_fill_value = varargin{n+1}; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
281 case "expchars" |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
282 warning ("strread: property 'expchars' is not implemented"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
283 case "whitespace" |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
284 white_spaces = varargin{n+1}; |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
285 if (strcmp (typeinfo (white_spaces), "sq_string")) |
13715
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
286 white_spaces = do_string_escapes (white_spaces); |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
287 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
288 ## The following parameters are specific to textscan and textread |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
289 case "endofline" |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
290 eol_char = varargin{n+1}; |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
291 if (strcmp (typeinfo (eol_char), "sq_string")) |
13715
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
292 eol_char = do_string_escapes (eol_char); |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
293 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
294 case "returnonerror" |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
295 err_action = varargin{n+1}; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
296 case "multipledelimsasone" |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
297 mult_dlms_s1 = varargin{n+1}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
298 case "treatasempty" |
12879
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
299 if (iscellstr (varargin{n+1})) |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
300 empty_str = varargin{n+1}; |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
301 elseif (ischar (varargin{n+1})) |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
302 empty_str = varargin(n+1); |
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
303 else |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
304 error ("strread: 'treatasempty' value must be string or cellstr"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
305 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
306 otherwise |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
307 warning ("strread: unknown property '%s'", varargin{n}); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
308 endswitch |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
309 endfor |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
310 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
311 ## First parse of FORMAT |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
312 if (strcmpi (strtrim (format), "%f")) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
313 ## Default format specified. Expand it (to desired nargout) |
16306
4a3b6a80db2e
strread.m: fix division by zero when called with no output args; test added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16094
diff
changeset
|
314 fmt_words = cell (max (nargout, 1), 1); |
4a3b6a80db2e
strread.m: fix division by zero when called with no output args; test added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16094
diff
changeset
|
315 fmt_words (1:max (nargout, 1)) = format; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
316 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
317 ## Determine the number of words per line as a first guess. Forms |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
318 ## like %f<literal>) (w/o delimiter in between) are fixed further on |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
319 format = strrep (format, "%", " %"); |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
320 fmt_words = regexp (format, '[^ ]+', "match"); |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19050
diff
changeset
|
321 |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
322 ## Find position of conversion specifiers (they start with %) |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
323 fcs_ptrn = '(%\*?(\d*|\d*\.\d*)?[nfduscq]|%\*?\[)'; |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
324 idy2 = find (! cellfun ("isempty", regexp (fmt_words, fcs_ptrn))); |
14802
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
325 |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
326 ## Check for unsupported format specifiers |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
327 errpat = '(\[.*\]|[cq]|[nfdu]8|[nfdu]16|[nfdu]32|[nfdu]64)'; |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
328 if (! all (cellfun ("isempty", regexp (fmt_words(idy2), errpat)))) |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
329 error ("strread: %q, %c, %[] or bit width format specifiers are not supported yet."); |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
330 endif |
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
331 |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
332 ## Format conversion specifiers following literals w/o space/delim |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
333 ## in between are separate now. Separate those w trailing literals |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
334 a = strfind (fmt_words(idy2), "%"); |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
335 b = regexp (fmt_words(idy2), '[nfdus]', "end"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
336 for jj = 1:numel (a) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
337 ## From right to left to avoid losing track |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
338 ii = numel (a) - jj + 1; |
16571
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
339 ## Check for illegal format specifiers |
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
340 if (isempty (b{ii})) |
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
341 error ("strread: unknown format specifier #%d ('%s')\n", |
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
342 ii, fmt_words{idy2(ii)}); |
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
343 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
344 if (! (length (fmt_words{idy2(ii)}) == b{ii}(1))) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
345 ## Split fmt_words(ii) into % conv specifier and trailing literal |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
346 fmt_words(idy2(ii)+1 : end+1) = fmt_words(idy2(ii) : end); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
347 fmt_words{idy2(ii)} = fmt_words{idy2(ii)}(a{ii} : b{ii}(1)); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
348 fmt_words{idy2(ii)+1} = fmt_words{idy2(ii)+1}(b{ii}+1:end); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
349 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
350 endfor |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
351 endif |
11127
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
352 num_words_per_line = numel (fmt_words); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
353 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
354 ## Special handling for CRLF EOL character in str |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
355 if (! isempty (eol_char) && strcmp (eol_char, "\r\n")) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
356 ## Strip CR from CRLF sequences |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
357 str = strrep (str, "\r\n", "\n"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
358 ## CR serves no further purpose in function |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
359 eol_char = "\n"; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
360 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
361 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
362 ## Remove comments in str |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
363 if (comment_flag) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
364 ## Expand 'eol_char' here, after option processing which may have set value |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
365 comment_end = regexprep (comment_end, "eol_char", eol_char); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
366 cstart = strfind (str, comment_start); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
367 cstop = strfind (str, comment_end); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
368 ## Treat end of string as additional comment stop |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
369 if (isempty (cstop) || cstop(end) != length (str)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
370 cstop(end+1) = length (str); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
371 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
372 if (! isempty (cstart)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
373 ## Ignore nested openers. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
374 [idx, cidx] = unique (lookup (cstop, cstart), "first"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
375 if (idx(end) == length (cstop)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
376 cidx(end) = []; # Drop the last one if orphaned. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
377 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
378 cstart = cstart(cidx); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
379 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
380 if (! isempty (cstop)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
381 ## Ignore nested closers. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
382 [idx, cidx] = unique (lookup (cstart, cstop), "first"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
383 if (idx(1) == 0) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
384 cidx(1) = []; # Drop the first one if orphaned. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
385 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
386 cstop = cstop(cidx); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
387 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
388 len = length (str); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
389 c2len = length (comment_end); |
19899
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
390 if (cstop + c2len == len) |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
391 ## Ignore last char of to-the-end-of-line comments |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
392 c2len++; |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
393 end |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
394 str = cellslices (str, [1, cstop + c2len], [cstart - 1, len]); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
395 str = [str{:}]; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
396 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
397 |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
398 if (! isempty (white_spaces)) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
399 ## For numeric fields, whitespace is always a delimiter, but not for text |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
400 ## fields |
20652
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
401 if (isempty (regexp (format, '%\*?\d*s'))) |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
402 ## Add whitespace to delimiter set |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
403 delimiter_str = unique ([white_spaces delimiter_str]); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
404 else |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
405 ## Remove any delimiter chars from white_spaces list |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
406 white_spaces = setdiff (white_spaces, delimiter_str); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
407 endif |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
408 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
409 if (isempty (delimiter_str)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
410 delimiter_str = " "; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
411 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
412 if (! isempty (eol_char)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
413 ## Add eol_char to delimiter collection |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
414 delimiter_str = unique ([delimiter_str eol_char]); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
415 ## .. and remove it from whitespace collection |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
416 white_spaces = strrep (white_spaces, eol_char, ''); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
417 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
418 |
19049
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
419 ii = numel (fmt_words); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
420 while (ii > 0) |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
421 if (ismember (fmt_words{ii}, delimiter_str)(1)) |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
422 fmt_words(ii) = []; |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
423 --num_words_per_line; |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
424 endif |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
425 --ii; |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
426 endwhile |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
427 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
428 pad_out = 0; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
429 ## Trim whitespace if needed |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
430 if (! isempty (white_spaces)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
431 ## Check if trailing "\n" might signal padding output arrays to equal size |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
432 ## before it is trimmed away below |
18146
f8b7e8483b88
avoid possible num-to-str warnings from strread.m
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
433 if (str(end) == "\n" && nargout > 1) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
434 pad_out = 1; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
435 endif |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
436 ## Condense all repeated whitespace into one single space |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
437 ## FIXME: this will also fold repeated whitespace in a char field |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
438 rxp_wsp = sprintf ("[%s]+", white_spaces); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
439 str = regexprep (str, rxp_wsp, ' '); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
440 ## Remove possible leading space at string |
18146
f8b7e8483b88
avoid possible num-to-str warnings from strread.m
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
441 if (str(1) == " ") |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
442 str = str(2:end); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
443 endif |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
444 ## Check for single delimiter followed/preceded by whitespace |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
445 if (! isempty (delimiter_str)) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
446 dlmstr = setdiff (delimiter_str, " "); |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
447 if (! isempty (dlmstr)) |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
448 rxp_dlmwsp = sprintf ('( [%s] | [%s]|[%s] )', dlmstr, dlmstr, dlmstr); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
449 str = regexprep (str, rxp_dlmwsp, delimiter_str(1)); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
450 endif |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
451 endif |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
452 ## Wipe leading and trailing whitespace on each line (it may be |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
453 ## delimiter too) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
454 ## FIXME: Double strrep on str is enormously expensive of CPU time. |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
455 ## Can this be eliminated |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
456 if (! isempty (eol_char)) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
457 str = strrep (str, [eol_char " "], eol_char); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
458 str = strrep (str, [" " eol_char], eol_char); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
459 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
460 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
461 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
462 ## Split 'str' into words |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
463 words = split_by (str, delimiter_str, mult_dlms_s1, eol_char); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
464 if (! isempty (white_spaces)) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
465 ## Trim leading and trailing 'white_spaces'. All whitespace has |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
466 ## been converted to space above |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
467 words = strtrim (words); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
468 endif |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
469 num_words = numel (words); |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
470 ## First guess at nr. of lines in file (ignoring leading/trailing literals) |
12879
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
471 num_lines = ceil (num_words / num_words_per_line); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11575
diff
changeset
|
472 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
473 ## Replace TreatAsEmpty char sequences by empty strings |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
474 if (! isempty (empty_str)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
475 for ii = 1:numel (empty_str) |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
476 idz = strncmp (empty_str{ii}, words, length (empty_str{ii})); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
477 words(idz) = {""}; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
478 endfor |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
479 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
480 |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
481 ## fmt_words has been split properly now, but words{} has only been split on |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
482 ## delimiter positions. As numeric fields can also be separated by |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
483 ## whitespace, more splits may be needed. |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
484 ## We also don't know the number of lines (as EndOfLine may have been set to |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
485 ## "" (empty) by the caller). |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
486 ## |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
487 ## We also may have to cope with 3 cases as far as literals go: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
488 ## A: Trailing literals (%f<literal>) w/o delimiter in between. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
489 ## B: Leading literals (<literal>%f) w/o delimiter in between. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
490 ## C. Skipping leftover parts of specified skip fields (%*N ) |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
491 ## Some words columns may have to be split further to fix these. |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
492 ## To find out, we'll match fmt_words to the words array to see what |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
493 ## needs to be done. fwptr tracks which {fmt_words}# starts in what {words}# |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
494 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
495 ## Find indices and pointers to possible literals in fmt_words |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
496 idf = cellfun ("isempty", strfind (fmt_words, "%")); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
497 ## Find indices and pointers to conversion specifiers with fixed width |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
498 idg = ! cellfun ("isempty", regexp (fmt_words, '%\*?\d')); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
499 idy = find (idf | idg); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
500 ## Find indices to numeric conversion specifiers |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
501 idn = ! cellfun ("isempty", regexp (fmt_words, '%[dnfu]')); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
502 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
503 ## If needed, split up columns in three steps: |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
504 if (! isempty (idy)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
505 ## Try-catch because complexity of strings to read can be infinite |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
506 try |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
507 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
508 ## 1. Assess "period" in the split-up words array ( < num_words_per_line). |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
509 ## Could be done using EndOfLine but that prohibits EndOfLine = "" option. |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
510 ## Alternative below goes by simply parsing a first grab of words and |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
511 ## matching fmt_words to words until the fmt_words array is exhausted. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
512 ## iwrd: ptr to current analyzed word. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
513 ## iwrdp: ptr to pos before analyzed char. |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
514 iwrd = 1; iwrdp = 0; iwrdl = length (words{1}); |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
515 fwptr = zeros (1, numel (fmt_words)); |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
516 ii = 1; |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
517 while (ii <= numel (fmt_words)) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
518 |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
519 nxt_wrd = 0; |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
520 ## Keep track of which words nr. every fmt_words{} is (starts) in. |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
521 fwptr(ii) = iwrd; |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
522 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
523 if (idf(ii)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
524 ## Literal expected |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
525 if (isempty (strfind (fmt_words{ii}, words(iwrd)))) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
526 ## Not found in current word; supposed to be in next word |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
527 nxt_wrd = 1; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
528 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
529 ## Found it in current word. Subtract literal length |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
530 iwrdp += length (fmt_words{ii}); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
531 if (iwrdp > iwrdl) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
532 ## Parse error. Literal extends beyond delimiter (word boundary) |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
533 warning ("strread: literal '%s' (fmt spec # %d) does not match data", ... |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
534 fmt_words{ii}, ii); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
535 ## Word assumed to be completely "used up". Next word |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
536 nxt_wrd = 1; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
537 elseif (iwrdp == iwrdl) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
538 ## Word completely "used up". Next word |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
539 nxt_wrd = 1; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
540 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
541 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
542 |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
543 elseif (idg(ii)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
544 ## Fixed width specifier (%N or %*N): read just a part of word |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
545 sw = regexp (fmt_words{ii}, '\d', "once"); |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
546 ew = regexp (fmt_words{ii}, '[nfuds]') - 1; |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
547 iwrdp += floor (str2double (fmt_words{ii}(sw:ew))); |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
548 if (iwrdp > iwrdl) |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
549 ## Match error. Field extends beyond word boundary. |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
550 warning ... |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
551 ("strread: field width '%s' (fmt spec # %d) extends beyond actual word limit", ... |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
552 fmt_words{ii}, ii); |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
553 ## Assume word to be completely "used up". Next word |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
554 nxt_wrd = 1; |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
555 elseif (iwrdp == iwrdl) |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
556 ## Word completely "used up". Next word |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
557 nxt_wrd = 1; |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
558 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
559 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
560 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
561 ## A simple format conv. specifier. Either (1) uses rest of word, or |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
562 ## (2) is squeezed between current iwrdp and next literal, or (3) uses |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
563 ## next word. (3) is already taken care of. So just check (1) & (2) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
564 if (ii < numel (fmt_words) && idf(ii+1)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
565 ## Next fmt_word is a literal... |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
566 if (! index (words{iwrd}(iwrdp+1:end), fmt_words{ii+1})) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
567 ## ...but not found in current word => field uses rest of word |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
568 nxt_wrd = 1; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
569 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
570 ## ..or it IS found. Add inferred width of current conversion field |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
571 iwrdp += index (words{iwrd}(iwrdp+1:end), fmt_words{ii+1}) - 1; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
572 endif |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
573 elseif (iwrdp <= iwrdl) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
574 ## No bordering literal to the right => field occupies (rest of) word |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
575 nxt_wrd = 1; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
576 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
577 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
578 endif |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
579 |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
580 if (nxt_wrd) |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
581 ++iwrd; iwrdp = 0; |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
582 if (iwrd > numel (words)) |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
583 ## Apparently EOF; assume incomplete row already at L.1 of data |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
584 ii = numel (fmt_words); |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
585 elseif (ii < numel (fmt_words) && iwrd <= numel (words)) |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
586 iwrdl = length (words{iwrd}); |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
587 endif |
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
588 endif |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
589 |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
590 ++ii; |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
591 |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
592 endwhile |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
593 ## Done |
12940
3509cf60d1f6
strread.m: %Ns conversion format now honors user specified width (Bug #33950).
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
12931
diff
changeset
|
594 words_period = max (iwrd - 1, 1); |
12879
c3b305e7e59f
maint: Reverse previous changeset 875c735c0929
Rik <octave@nomad.inbox5.com>
parents:
12878
diff
changeset
|
595 num_lines = ceil (num_words / words_period); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
596 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
597 ## 2. Pad words array so that it can be reshaped |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
598 num_words_padded = num_lines * words_period - num_words; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
599 if (num_words_padded) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
600 words = [words'; cell(num_words_padded, 1)]; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
601 endif |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
602 words = reshape (words, words_period, num_lines); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
603 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
604 ## 3. Do the column splitting on rectangular words array |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
605 icol = 1; ii = 1; # icol = current column, ii = current fmt_word |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
606 while (ii <= num_words_per_line) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
607 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
608 ## Check if fmt_words(ii) contains a literal or fixed-width |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
609 if ((idf(ii) || idg(ii)) && (rows (words) < num_words_per_line)) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
610 if (idf(ii)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
611 s = strfind (words(icol, 1), fmt_words{ii}); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
612 if (isempty (s{:})) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
613 error ("strread: Literal '%s' not found in column %d", fmt_words{ii}, icol); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
614 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
615 s = s{:}(1); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
616 e = s(1) + length (fmt_words{ii}) - 1; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
617 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
618 if (! strcmp (fmt_words{ii}, words{icol, 1})) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
619 ## Column doesn't exactly match literal => split needed. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
620 ## Insert a column |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
621 words(icol+1:end+1, :) = words(icol:end, :); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
622 ## Watch out for empty cells |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
623 jptr = find (! cellfun ("isempty", words(icol, :))); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
624 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
625 ## Distinguish leading or trailing literals |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
626 if (! idg(ii) && ! isempty (s) && s(1) == 1) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
627 ## Leading literal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
628 ## Assign literal to icol, paste rest in icol + 1 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
629 ## Apply only to those cells that do have something beyond literal |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
630 jptr = find (cellfun ("length", words(icol+1, jptr), ... |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
631 "UniformOutput", false) > e(1)); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
632 words(icol+1, :) = {""}; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
633 words(icol+1, jptr) = cellfun ( |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
634 @(x) substr (x, e(1)+1, length (x) - e(1)), words(icol, jptr), |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
635 "UniformOutput", false); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
636 words(icol, jptr) = fmt_words{ii}; |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
637 fwptr = [fwptr(1:ii) (++fwptr(ii+1:end))]; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
638 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
639 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
640 if (! idg(ii) && ! isempty (strfind (fmt_words{ii-1}, "%s"))) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
641 ## Trailing literal. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
642 ## If preceding format == '%s' this is an error. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
643 warning ("strread: ambiguous '%s' specifier next to literal in column %d", icol); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
644 elseif (idg(ii)) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
645 ## Current field = fixed width. Strip into icol, rest in icol+1 |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
646 sw = regexp (fmt_words{ii}, '\d', "once"); |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
647 ew = regexp (fmt_words{ii}, '[nfuds]') - 1; |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
648 wdth = floor (str2double (fmt_words{ii}(sw:ew))); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
649 words(icol+1, jptr) = cellfun (@(x) x(wdth+1:end), |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
650 words(icol,jptr), "UniformOutput", false); |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
651 if (isempty ([words(icol+1, :){:}])) |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
652 ## Apparently split wasn't needed as turns out to cover |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
653 ## entire column. So delete column again |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
654 words(icol+1, :) = []; |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
655 else |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
656 words(icol, jptr) = strtrunc (words(icol, jptr), wdth); |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
657 fwptr = [fwptr(1:ii) (++fwptr(ii+1:end))]; |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
658 endif |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
659 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
660 ## FIXME: this assumes char(254)/char(255) won't occur in input! |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
661 clear wrds; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
662 wrds(1:2:2*numel (words(icol, jptr))) = ... |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
663 strrep (words(icol, jptr), fmt_words{ii}, ... |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
664 [char(255) char(254)]); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
665 wrds(2:2:2*numel (words(icol, jptr))-1) = char (255); |
16724
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16571
diff
changeset
|
666 wrds = ostrsplit ([wrds{:}], char (255)); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
667 words(icol, jptr) = ... |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
668 wrds(find (cellfun ("isempty", strfind (wrds, char (254))))); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
669 wrds(find (cellfun ("isempty", strfind (wrds, char (254))))) ... |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
670 = char (255); |
16724
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16571
diff
changeset
|
671 words(icol+1, jptr) = ostrsplit (strrep ([wrds{2:end}], ... |
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16571
diff
changeset
|
672 char (254), fmt_words{ii}), char (255)); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
673 ## Former trailing literal may now be leading for next specifier |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
674 --ii; |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
675 fwptr = [fwptr(1:ii) (++fwptr(ii+1:end))]; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
676 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
677 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
678 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
679 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
680 else |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
681 ## Conversion specifier. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
682 ## Peek if next fmt_word needs split from current column. |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
683 if (ii < num_words_per_line) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
684 if (fwptr(ii) == fwptr(ii+1)) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
685 --icol; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
686 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
687 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
688 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
689 ## Next fmt_word, next column |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
690 ++ii; ++icol; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
691 endwhile |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
692 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
693 ## Done. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
694 ## Reshape words back into one long vector and strip padded empty words |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
695 words = reshape (words, 1, numel (words))(1 : end-num_words_padded); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
696 |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
697 catch |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
698 warning ("strread: unable to parse text or file with given format string"); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
699 return; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
700 |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
701 end_try_catch |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
702 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
703 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
704 ## For each specifier, process corresponding column |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
705 k = 1; |
11127
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
706 for m = 1:num_words_per_line |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
707 try |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
708 if (format_repeat_count < 0) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
709 data = words(m:num_words_per_line:end); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
710 elseif (format_repeat_count == 0) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
711 data = {}; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
712 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
713 lastline = ... |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
714 min (num_words_per_line * format_repeat_count + m - 1, numel (words)); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
715 data = words(m:num_words_per_line:lastline); |
20419
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
716 if (num_lines > format_repeat_count) |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
717 num_lines = format_repeat_count; |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
718 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
719 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
720 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
721 ## Map to format |
19047
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
722 ## FIXME: Add support for formats like "<%s>", "%[a-zA-Z]" |
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
723 ## Someone with regexp experience is needed. |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
724 switch (fmt_words{m}(1:min (2, length (fmt_words{m})))) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
725 case "%s" |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
726 if (pad_out) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
727 data(end+1:num_lines) = {""}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
728 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
729 varargout{k} = data'; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
730 k++; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
731 case {"%d", "%u", "%f", "%n"} |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
732 n = cellfun ("isempty", data); |
19047
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
733 ### FIXME: Erroneously formatted data lead to NaN, not an error |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
734 data = str2double (data); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
735 if (! isempty (regexp (fmt_words{m}, "%[du]"))) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
736 ## Cast to integer |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
737 ## FIXME: NaNs will be transformed into zeros |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
738 data = int32 (data); |
13176
9b8e786bbf3c
maint: use specific endif, endfor tokens instead of simple end
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
739 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
740 data(n) = numeric_fill_value; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
741 if (pad_out) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
742 data(end+1:num_lines) = numeric_fill_value; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
743 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
744 varargout{k} = data.'; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
745 k++; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
746 case {"%0", "%1", "%2", "%3", "%4", "%5", "%6", "%7", "%8", "%9"} |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
747 sw = regexp (fmt_words{m}, '\d', "once"); |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
748 ew = regexp (fmt_words{m}, '[nfudsq]') - 1; |
16724
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16571
diff
changeset
|
749 nfmt = ostrsplit (fmt_words{m}(2:ew), "."); |
12873
e8c8e118a1e6
Small touchups to textscan and strread revamp
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
750 swidth = str2double (nfmt{1}); |
16933
e39f00a32dc7
maint: Use parentheses around condition for switch(),while(),if() statements.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
751 switch (fmt_words{m}(ew+1)) |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
752 case {"d", "u", "f", "n"} |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
753 n = cellfun ("isempty", data); |
19047
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
754 ### FIXME: Erroneously formatted data lead to NaN, not an error |
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
18146
diff
changeset
|
755 ### => ReturnOnError can't be implemented for numeric data |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
756 data = str2double (strtrunc (data, swidth)); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
757 data(n) = numeric_fill_value; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
758 if (pad_out) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
759 data(end+1:num_lines) = numeric_fill_value; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
760 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
761 if (numel (nfmt) > 1) |
12873
e8c8e118a1e6
Small touchups to textscan and strread revamp
Rik <octave@nomad.inbox5.com>
parents:
12866
diff
changeset
|
762 sprec = str2double (nfmt{2}); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
763 data = 10^-sprec * round (10^sprec * data); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
764 elseif (! isempty (regexp (fmt_words{m}, "[du]"))) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
765 ## Cast to integer |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
766 ## FIXME: NaNs will be transformed into zeros |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
767 data = int32 (data); |
13176
9b8e786bbf3c
maint: use specific endif, endfor tokens instead of simple end
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
768 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
769 varargout{k} = data.'; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
770 k++; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
771 case "s" |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
772 if (pad_out) |
14317
7dee2bb231c1
strread.m: Fix missing semicolon and unnecessary "unable to parse" messages (Bug #35418).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14138
diff
changeset
|
773 data(end+1:num_lines) = {""}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
774 endif |
12940
3509cf60d1f6
strread.m: %Ns conversion format now honors user specified width (Bug #33950).
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
12931
diff
changeset
|
775 varargout{k} = strtrunc (data, swidth)'; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
776 k++; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
777 otherwise |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
778 endswitch |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
779 case {"%*", "%*s"} |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
780 ## skip the word |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
781 otherwise |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
782 ## Ensure descriptive content is consistent. |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
783 ## Test made a bit lax to accomodate for incomplete last lines |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
784 n = find (! cellfun ("isempty", data)); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
785 if (numel (unique (data(n))) > 1 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
786 || ! strcmpi (unique (data), fmt_words{m})) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
787 error ("strread: FORMAT does not match data"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
788 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
789 endswitch |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
790 catch |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
791 ## As strread processes columnwise, ML-compatible error processing |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
792 ## (row after row) is not feasible. In addition Octave sets unrecognizable |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
793 ## numbers to NaN w/o error. But maybe Octave is better in this respect. |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
794 if (err_action) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
795 ## Just try the next column where ML bails out |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
796 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
797 rethrow (lasterror); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
798 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
799 end_try_catch |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
800 endfor |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
801 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
802 endfunction |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
803 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
804 function out = split_by (text, sep, mult_dlms_s1, eol_char) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
805 |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
806 ## Check & if needed, process MultipleDelimsAsOne parameter |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
807 if (mult_dlms_s1) |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
808 mult_dlms_s1 = true; |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
809 ## FIXME: Should re-implement strsplit() function here in order |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
810 ## to avoid strrep on megabytes of data. |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
811 ## If \n is in sep collection we need to enclose it in text |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
812 ## to avoid it being included in consecutive delim series |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
813 enchr = ' '; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
814 ## However watch out if eol_char is also in delimiters |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
815 if (index (sep, eol_char)); enchr = char (255); endif |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
816 text = strrep (text, eol_char, [enchr eol_char enchr]); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
817 else |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
818 mult_dlms_s1 = false; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
819 endif |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
820 |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
821 ## Split text string along delimiters |
16724
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16571
diff
changeset
|
822 out = ostrsplit (text, sep, mult_dlms_s1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
823 if (index (sep, eol_char)); out = strrep (out, char (255), ''); endif |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
824 ## In case of trailing delimiter, strip stray last empty word |
20245
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
825 if (! isempty (out) && any (sep == text(end)) && ! mult_dlms_s1) |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
826 out(end) = []; |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
827 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12940
diff
changeset
|
828 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
829 ## Empty cells converted to empty cellstrings. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
830 out(cellfun ("isempty", out)) = {""}; |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
831 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
832 endfunction |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
833 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
834 |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
835 %!test |
11343
5e5c513ea4c5
strread.m: Don't require space between format specifiers.
Ben Abbott <bpabbott@mac.com>
parents:
11150
diff
changeset
|
836 %! [a, b] = strread ("1 2", "%f%f"); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
837 %! assert (a, 1); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
838 %! assert (b, 2); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
839 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
840 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
841 %! str = ""; |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
842 %! a = rand (10, 1); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
843 %! b = char (randi ([65, 85], 10, 1)); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
844 %! for k = 1:10 |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
845 %! str = sprintf ("%s %.6f %s\n", str, a(k), b(k)); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
846 %! endfor |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
847 %! [aa, bb] = strread (str, "%f %s"); |
15844
8f0d07f8390b
Fix assertions with "observed" and "expected" out of order.
Ben Abbott <bpabbott@mac.com>
parents:
15263
diff
changeset
|
848 %! assert (aa, a, 1e-6); |
8f0d07f8390b
Fix assertions with "observed" and "expected" out of order.
Ben Abbott <bpabbott@mac.com>
parents:
15263
diff
changeset
|
849 %! assert (bb, cellstr (b)); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
850 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
851 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
852 %! str = ""; |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
853 %! a = rand (10, 1); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
854 %! b = char (randi ([65, 85], 10, 1)); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
855 %! for k = 1:10 |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
856 %! str = sprintf ("%s %.6f %s\n", str, a(k), b(k)); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
857 %! endfor |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
858 %! aa = strread (str, "%f %*s"); |
15844
8f0d07f8390b
Fix assertions with "observed" and "expected" out of order.
Ben Abbott <bpabbott@mac.com>
parents:
15263
diff
changeset
|
859 %! assert (aa, a, 1e-6); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
860 |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
861 %!test |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
862 %! str = sprintf ("/* this is\nacomment*/ 1 2 3"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
863 %! a = strread (str, "%f", "commentstyle", "c"); |
9746
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
864 %! assert (a, [1; 2; 3]); |
31a22d48f41f
scripts/io/strread.m scripts/io/textread.m: new functions
Soren Hauberg <hauberg@gmail.com>
parents:
diff
changeset
|
865 |
11127
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
866 %!test |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
867 %! str = "# comment\n# comment\n1 2 3"; |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
868 %! [a, b] = strread (str, "%n %s", "commentstyle", "shell", "endofline", "\n"); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
869 %! assert (a, [1; 3]); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
870 %! assert (b, {"2"}); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
871 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
872 %!test |
19899
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
873 %! assert (strread ("Hello World! // this is comment", "%s",... |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
874 %! "commentstyle", "c++"), ... |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
875 %! {"Hello"; "World!"}); |
19899
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
876 %! assert (strread ("Hello World! % this is comment", "%s",... |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
877 %! "commentstyle", "matlab"), ... |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
878 %! {"Hello"; "World!"}); |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
879 %! assert (strread ("Hello World! # this is comment", "%s",... |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
880 %! "commentstyle", "shell"), ... |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
881 %! {"Hello"; "World!"}); |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
882 |
4aa17069a007
Fix incorrect strread behavior with option "commentstyle" (bug #43972)
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
19898
diff
changeset
|
883 %!test |
11127
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
884 %! str = sprintf ("Tom 100 miles/hr\nDick 90 miles/hr\nHarry 80 miles/hr"); |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
885 %! fmt = "%s %f miles/hr"; |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
886 %! c = cell (1, 2); |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
887 %! [c{:}] = strread (str, fmt); |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
888 %! assert (c{1}, {"Tom"; "Dick"; "Harry"}) |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
889 %! assert (c{2}, [100; 90; 80]) |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
890 |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
891 %!test |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
892 %! a = strread ("a b c, d e, , f", "%s", "delimiter", ","); |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
893 %! assert (a, {"a b c"; "d e"; ""; "f"}); |
64728cd28d7a
strread.m: Improve compatibility with Matlab.
Ben Abbott <bpabbott@mac.com>
parents:
11120
diff
changeset
|
894 |
20419
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
895 %! ## Format repeat counters w & w/o trailing EOL even within partly read files |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
896 %!test |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
897 %! [a, b] = strread ("10 a 20 b\n 30 c 40", "%d %s", 4); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
898 %! assert (a, int32 ([10; 20; 30; 40])); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
899 %! assert (b, {"a"; "b"; "c"}); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
900 %! [a, b] = strread ("10 a 20 b\n 30 c 40\n", "%d %s", 4); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
901 %! assert (a, int32 ([10; 20; 30; 40])); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
902 %! assert (b, {"a"; "b"; "c"; ""}); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
903 %! [a, b] = strread ("10 a 20 b\n 30 c 40", "%d %s", 1); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
904 %! assert (a, int32 (10)); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
905 %! assert (b, {"a"}); |
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
906 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
907 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
908 %! ## Bug #33536 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
909 %! [a, b, c] = strread ("1,,2", "%s%s%s", "delimiter", ","); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
910 %! assert (a{1}, "1"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
911 %! assert (b{1}, ""); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
912 %! assert (c{1}, "2"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
913 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
914 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
915 %! ## Bug #33536 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
916 %! a = strread ("[SomeText]", "[%s", "delimiter", "]"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
917 %! assert (a{1}, "SomeText"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
918 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
919 %!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
920 %! dat = "Data file.\r\n= = = = =\r\nCOMPANY : <Company name>\r\n"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
921 %! a = strread (dat, "%s", "delimiter", "\n", "whitespace", "", "endofline", "\r\n"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
922 %! assert (a{2}, "= = = = ="); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
923 %! assert (double (a{3}(end-5:end)), [32 110 97 109 101 62]); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
924 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
925 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
926 %! [a, b, c, d] = strread ("1,2,3,,5,6", "%d%f%d%f", "delimiter", ","); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
927 %! assert (c, int32 (3)); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
928 %! assert (d, NaN); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
929 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
930 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
931 %! [a, b, c, d] = strread ("1,2,3,,5,6\n", "%d%d%f%d", "delimiter", ","); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
932 %! assert (c, [3; NaN]); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
933 %! assert (d, int32 ([0; 0])); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
934 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
935 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
936 %! ## Default format (= %f) |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
937 %1 [a, b, c] = strread ("0.12 0.234 0.3567"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
938 %1 assert (a, 0.12); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
939 %1 assert (b, 0.234); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
940 %1 assert (c, 0.3567); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
941 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
942 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
943 %! [a, b] = strread ("0.41 8.24 3.57 6.24 9.27", "%f%f", 2, "delimiter", " "); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
944 %1 assert (a, [0.41; 3.57]); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
945 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
946 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
947 %! ## TreatAsEmpty |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
948 %! [a, b, c, d] = strread ("1,2,3,NN,5,6\n", "%d%d%d%f", "delimiter", ",", "TreatAsEmpty", "NN"); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
949 %! assert (c, int32 ([3; 0])); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
950 %! assert (d, [NaN; NaN]); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
951 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
952 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
953 %! ## No delimiters at all besides EOL. Plain reading numbers & strings |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
954 %! str = "Text1Text2Text\nText398Text4Text\nText57Text"; |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
955 %! [a, b] = strread (str, "Text%dText%1sText"); |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
956 %! assert (a, int32 ([1; 398; 57])); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
957 %! assert (b(1:2), {"2"; "4"}); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
958 %! assert (isempty (b{3}), true); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12469
diff
changeset
|
959 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
960 ## MultipleDelimsAsOne |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
961 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
962 %! str = "11, 12, 13,, 15\n21,, 23, 24, 25\n,, 33, 34, 35"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
963 %! [a b c d] = strread (str, "%f %f %f %f", "delimiter", ",", "multipledelimsasone", 1, "endofline", "\n"); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
964 %! assert (a', [11, 21, NaN]); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
965 %! assert (b', [12, 23, 33]); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
966 %! assert (c', [13, 24, 34]); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
967 %! assert (d', [15, 25, 35]); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12879
diff
changeset
|
968 |
20245
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
969 ## Bug #44750 |
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
970 %!test |
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
971 %! assert (strread ('/home/foo/','%s','delimiter','/','MultipleDelimsAsOne',1), ... |
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
972 %! {"home"; "foo"}); |
f68c3a62e42c
strread.m: properly process traling delimiters in case of
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20072
diff
changeset
|
973 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
974 ## delimiter as sq_string and dq_string |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
975 %!test |
13715
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
976 %! assert (strread ("1\n2\n3", "%d", "delimiter", "\n"), |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
977 %! strread ("1\n2\n3", "%d", "delimiter", '\n')) |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
978 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
979 ## whitespace as sq_string and dq_string |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
980 %!test |
13715
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
981 %! assert (strread ("1\b2\r3\b4\t5", "%d", "whitespace", "\b\r\n\t"), |
f8eca611e18f
When single-quoted, the strread() "delimiter", "endofline", and "whitespace"
Ben Abbott <bpabbott@mac.com>
parents:
13176
diff
changeset
|
982 %! strread ("1\b2\r3\b4\t5", "%d", "whitespace", '\b\r\n\t')) |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
983 |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
984 %!test |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
985 %! str = "0.31 0.86 0.94\n 0.60 0.72 0.87"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
986 %! fmt = "%f %f %f"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
987 %! args = {"delimiter", " ", "endofline", "\n", "whitespace", " "}; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
988 %! [a, b, c] = strread (str, fmt, args{:}); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
989 %! assert (a, [0.31; 0.60], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
990 %! assert (b, [0.86; 0.72], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
991 %! assert (c, [0.94; 0.87], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
992 |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
993 %!test |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
994 %! str = "0.31,0.86,0.94\n0.60,0.72,0.87"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
995 %! fmt = "%f %f %f"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
996 %! args = {"delimiter", ",", "endofline", "\n", "whitespace", " "}; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
997 %! [a, b, c] = strread (str, fmt, args{:}); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
998 %! assert (a, [0.31; 0.60], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
999 %! assert (b, [0.86; 0.72], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1000 %! assert (c, [0.94; 0.87], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1001 |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1002 %!test |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1003 %! str = "0.31 0.86 0.94\n 0.60 0.72 0.87"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1004 %! fmt = "%f %f %f"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1005 %! args = {"delimiter", ",", "endofline", "\n", "whitespace", " "}; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1006 %! [a, b, c] = strread (str, fmt, args{:}); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1007 %! assert (a, [0.31; 0.60], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1008 %! assert (b, [0.86; 0.72], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1009 %! assert (c, [0.94; 0.87], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1010 |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1011 %!test |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1012 %! str = "0.31, 0.86, 0.94\n 0.60, 0.72, 0.87"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1013 %! fmt = "%f %f %f"; |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1014 %! args = {"delimiter", ",", "endofline", "\n", "whitespace", " "}; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1015 %! [a, b, c] = strread (str, fmt, args{:}); |
14096
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1016 %! assert (a, [0.31; 0.60], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1017 %! assert (b, [0.86; 0.72], 0.01) |
153581342e47
Always separate numeric fields by whitespace too; gracefully catch too wide literals & fixed width format conversion specifiers
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
13715
diff
changeset
|
1018 %! assert (c, [0.94; 0.87], 0.01) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
1019 |
14497
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1020 %!test |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1021 %! [a, b] = strread (["Empty 1" char(10)], "Empty%s %f"); |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1022 %! assert (a{1}, '1'); |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1023 %! assert (b, NaN); |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1024 |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1025 %!test |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1026 %! [a, b] = strread (["Empty" char(10)], "Empty%f %f"); |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1027 %! assert (a, NaN); |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1028 %! assert (b, NaN); |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1029 |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
1030 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1031 %! ## Bug #35999 |
14497
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1032 %! [a, b, c] = strread ("", "%f"); |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1033 %! assert (isempty (a)); |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1034 %! assert (isempty (b)); |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1035 %! assert (isempty (c)); |
9fc75cdf61ab
strread.m: Trap empty string input (bug #35999)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14366
diff
changeset
|
1036 |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1037 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1038 %! ## bug #37023 |
15263
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1039 %! [a, b] = strread (" 1. 1 \n 2 3 \n", "%f %f", "endofline", "\n"); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1040 %! assert (a, [1; 2], 1e-15); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1041 %! assert (b, [1; 3], 1e-15); |
2136343014d5
bug #37023 (wrong reading of lines starting and/or ending with whitespace)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14868
diff
changeset
|
1042 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1043 ## Test for no output arg (interactive use) |
16306
4a3b6a80db2e
strread.m: fix division by zero when called with no output args; test added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16094
diff
changeset
|
1044 %!test |
4a3b6a80db2e
strread.m: fix division by zero when called with no output args; test added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16094
diff
changeset
|
1045 %! assert (strread (",2,,4\n5,,7,", "", "delimiter", ","), [NaN; 2; NaN; 4; 5; NaN; 7]); |
4a3b6a80db2e
strread.m: fix division by zero when called with no output args; test added
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16094
diff
changeset
|
1046 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1047 ## Test #1 bug #42609 |
19049
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1048 %!test |
19050
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1049 %! [a, b, c] = strread ("1 2 3\n4 5 6\n7 8 9\n", "%f %f %f\n"); |
19049
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1050 %! assert (a, [1; 4; 7]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1051 %! assert (b, [2; 5; 8]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1052 %! assert (c, [3; 6; 9]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1053 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1054 ## Test #2 bug #42609 |
19049
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1055 %!test |
19050
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1056 %! [a, b, c] = strread ("1 2\n3\n4 5\n6\n7 8\n9\n", "%f %f\n%f"); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1057 %! assert (a, [1;4;7]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1058 %! assert (b, [2; 5; 8]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1059 %! assert (c, [3; 6; 9]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1060 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1061 ## Test #3 bug #42609 |
19050
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1062 %!test |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1063 %! [a, b, c] = strread ("1 2 3\n4 5 6\n7 8 9\n", '%f %f %f\n'); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1064 %! assert (a, [1; 4; 7]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1065 %! assert (b, [2; 5; 8]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1066 %! assert (c, [3; 6; 9]); |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1067 |
20419
eae5e4d58740
strread.m: properly process format repeat counter, add tests
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20391
diff
changeset
|
1068 ## Test #4 bug #42609 |
19050
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1069 %!test |
c32f378e08f3
* strread.m: Process string escapes on single quoted format (bug #42609)
John W. Eaton <jwe@octave.org>
parents:
19049
diff
changeset
|
1070 %! [a, b, c] = strread ("1 2\n3\n4 5\n6\n7 8\n9\n", '%f %f\n%f'); |
19049
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1071 %! assert (a, [1;4;7]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1072 %! assert (b, [2; 5; 8]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1073 %! assert (c, [3; 6; 9]); |
35838f49e2f6
strread.m: drop valid delimiters from format specifier list (bug #42609)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
17744
diff
changeset
|
1074 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1075 ## Unsupported format specifiers |
14802
d5aee269b770
strread.m: catch unsupported ML format specifiers (bug #36464)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14800
diff
changeset
|
1076 %!test |
14835
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1077 %!error <format specifiers are not supported> strread ("a", "%c") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1078 %!error <format specifiers are not supported> strread ("a", "%*c %d") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1079 %!error <format specifiers are not supported> strread ("a", "%q") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1080 %!error <format specifiers are not supported> strread ("a", "%*q %d") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1081 %!error <format specifiers are not supported> strread ("a", "%[a]") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1082 %!error <format specifiers are not supported> strread ("a", "%*[a] %d") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1083 %!error <format specifiers are not supported> strread ("a", "%[^a]") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1084 %!error <format specifiers are not supported> strread ("a", "%*[^a] %d") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1085 %!error <format specifiers are not supported> strread ("a", "%d8") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1086 %!error <format specifiers are not supported> strread ("a", "%*d8 %s") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1087 %!error <format specifiers are not supported> strread ("a", "%f64") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1088 %!error <format specifiers are not supported> strread ("a", "%*f64 %s") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1089 %!error <format specifiers are not supported> strread ("a", "%u32") |
821708f96efd
strread.m: more robust word parsing and style improvements
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14802
diff
changeset
|
1090 %!error <format specifiers are not supported> strread ("a", "%*u32 %d") |
16571
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
1091 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1092 ## Illegal format specifiers |
16571
4d8462fe15b9
strread.m: catch unknown format specifiers (bug #38834)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
16412
diff
changeset
|
1093 %!test |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
1094 %!error <no valid format conversion specifiers> strread ("1.0", "%z"); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17336
diff
changeset
|
1095 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19899
diff
changeset
|
1096 ## Test for false positives in check for non-supported format specifiers |
19241
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
1097 %!test |
c573d9c70ae5
Better checks for valid format conversion specifiers in textscan. & strread.m
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19058
diff
changeset
|
1098 %! assert (strread ("Total: 32.5 % (of cm values)","Total: %f % (of cm values)"), 32.5, 1e-5); |
20652
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1099 |
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1100 ## Test various forms of string format specifiers (bug #45712) |
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1101 %!test |
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1102 %! str = "14 :1 z:2 z:3 z:5 z:11"; |
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1103 %! [a, b, c, d] = strread (str, "%f %s %*s %3s %*3s %f", "delimiter", ":"); |
4c4d8fe5583a
strread.m: properly preprocess all forms of string format specifier (bug #45712)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
20419
diff
changeset
|
1104 %! assert ({a, b, c, d}, {14, {"1 z"}, {"3 z"}, 11}); |