Mercurial > hg > octave-nkf
annotate scripts/io/textscan.m @ 14868:5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
* lin2mu.m, loadaudio.m, wavread.m, accumarray.m, bicubic.m, celldisp.m,
colon.m, cplxpair.m, dblquad.m, divergence.m, genvarname.m, gradient.m,
int2str.m, interp1.m, interp1q.m, interp2.m, interpn.m, loadobj.m, nthargout.m,
__isequal__.m, __splinen__.m, quadgk.m, quadl.m, quadv.m, rat.m, rot90.m,
rotdim.m, saveobj.m, subsindex.m, triplequad.m, delaunay3.m, griddata.m,
inpolygon.m, tsearchn.m, voronoi.m, get_first_help_sentence.m, which.m,
gray2ind.m, pink.m, dlmwrite.m, strread.m, textread.m, textscan.m, housh.m,
ishermitian.m, issymmetric.m, krylov.m, logm.m, null.m, rref.m,
compare_versions.m, copyfile.m, dump_prefs.m, edit.m, fileparts.m,
getappdata.m, isappdata.m, movefile.m, orderfields.m, parseparams.m,
__xzip__.m, rmappdata.m, setappdata.m, swapbytes.m, unpack.m, ver.m, fminbnd.m,
fminunc.m, fsolve.m, glpk.m, lsqnonneg.m, qp.m, sqp.m, configure_make.m,
copy_files.m, describe.m, get_description.m, get_forge_pkg.m, install.m,
installed_packages.m, is_architecture_dependent.m, load_package_dirs.m,
print_package_description.m, rebuild.m, repackage.m, save_order.m, shell.m,
allchild.m, ancestor.m, area.m, axes.m, axis.m, clabel.m, close.m, colorbar.m,
comet.m, comet3.m, contour.m, cylinder.m, ezmesh.m, ezsurf.m, findobj.m,
fplot.m, hist.m, isocolors.m, isonormals.m, isosurface.m, isprop.m, legend.m,
mesh.m, meshz.m, pareto.m, pcolor.m, peaks.m, plot3.m, plotmatrix.m, plotyy.m,
polar.m, print.m, __add_datasource__.m, __add_default_menu__.m,
__axes_limits__.m, __bar__.m, __clabel__.m, __contour__.m, __errcomm__.m,
__errplot__.m, __ezplot__.m, __file_filter__.m, __fltk_print__.m,
__ghostscript__.m, __gnuplot_print__.m, __go_draw_axes__.m,
__go_draw_figure__.m, __interp_cube__.m, __marching_cube__.m, __patch__.m,
__pie__.m, __plt__.m, __print_parse_opts__.m, __quiver__.m, __scatter__.m,
__stem__.m, __tight_eps_bbox__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiputfile_fltk__.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m,
ribbon.m, scatter.m, semilogy.m, shading.m, slice.m, subplot.m, surface.m,
surfl.m, surfnorm.m, text.m, uigetfile.m, uiputfile.m, whitebg.m, deconv.m,
mkpp.m, pchip.m, polyaffine.m, polyder.m, polygcd.m, polyout.m, polyval.m,
ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, splinefit.m,
addpref.m, getpref.m, setpref.m, ismember.m, setxor.m, arch_fit.m, arch_rnd.m,
arch_test.m, autoreg_matrix.m, diffpara.m, fftconv.m, filter2.m, hanning.m,
hurst.m, periodogram.m, triangle_sw.m, sinc.m, spectral_xdf.m, spencer.m,
stft.m, synthesis.m, unwrap.m, yulewalker.m, bicgstab.m, gmres.m, pcg.m, pcr.m,
__sprand_impl__.m, speye.m, spfun.m, sprandn.m, spstats.m, svds.m,
treelayout.m, treeplot.m, bessel.m, factor.m, legendre.m, perms.m, primes.m,
magic.m, toeplitz.m, corr.m, cov.m, mean.m, median.m, mode.m, qqplot.m,
quantile.m, ranks.m, zscore.m, logistic_regression_likelihood.m,
bartlett_test.m, chisquare_test_homogeneity.m, chisquare_test_independence.m,
kolmogorov_smirnov_test.m, run_test.m, u_test.m, wilcoxon_test.m, z_test.m,
z_test_2.m, bin2dec.m, dec2base.m, mat2str.m, strcat.m, strchr.m, strjust.m,
strtok.m, substr.m, untabify.m, assert.m, demo.m, example.m, fail.m, speed.m,
test.m, now.m: Use Octave coding conventions for cuddling parentheses in
scripts directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 17 Jul 2012 07:08:39 -0700 |
parents | 72b8b39e12be |
children | 2136343014d5 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
1 ## Copyright (C) 2010-2012 Ben Abbott <bpabbott@mac.com> |
11141 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
20 ## @deftypefn {Function File} {@var{C} =} textscan (@var{fid}, @var{format}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{n}) |
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{}) |
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{fid}, @var{format}, @var{n}, @var{param}, @var{value}, @dots{}) |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{C} =} textscan (@var{str}, @dots{}) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{C}, @var{position}] =} textscan (@var{fid}, @dots{}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
26 ## Read data from a text file or string. |
11141 | 27 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
28 ## The string @var{str} or file associated with @var{fid} is read from and |
14853
72b8b39e12be
doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14800
diff
changeset
|
29 ## parsed according to @var{format}. The function behaves like @code{strread} |
72b8b39e12be
doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14800
diff
changeset
|
30 ## except it can also read from file instead of a string. See the documentation |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
31 ## of @code{strread} for details. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
32 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
33 ## In addition to the options supported by @code{strread}, this function |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
34 ## supports a few more: |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
35 ## |
11141 | 36 ## @itemize |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
37 ## @item "collectoutput": |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
38 ## A value of 1 or true instructs textscan to concatenate consecutive columns |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
39 ## of the same class in the output cell array. A value of 0 or false (default) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
40 ## leaves output in distinct columns. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
41 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
42 ## @item "endofline": |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
43 ## Specify "\r", "\n" or "\r\n" (for CR, LF, or CRLF). If no value is given, |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
44 ## it will be inferred from the file. If set to "" (empty string) EOLs are |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
45 ## ignored as delimiters and added to whitespace. |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
46 ## |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
47 ## @item "headerlines": |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
48 ## The first @var{value} number of lines of @var{fid} are skipped. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
49 ## |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
50 ## @item "returnonerror": |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
51 ## If set to numerical 1 or true (default), return normally when read errors |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
52 ## have been encountered. If set to 0 or false, return an error and no data. |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
53 ## As the string or file is read by columns rather than by rows, and because |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
54 ## textscan is fairly forgiving as regards read errors, setting this option |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
55 ## may have little or no actual effect. |
11141 | 56 ## @end itemize |
57 ## | |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
58 ## When reading from a character string, optional input argument @var{n} |
14621
1804d5422f61
doc: Periodic spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14582
diff
changeset
|
59 ## specifies the number of times @var{format} should be used (i.e., to limit |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
60 ## the amount of data read). |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
61 ## When reading from file, @var{n} specifies the number of data lines to read; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
62 ## in this sense it differs slightly from the format repeat count in strread. |
11141 | 63 ## |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
64 ## The output @var{C} is a cell array whose second dimension is determined |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
65 ## by the number of format specifiers. |
11141 | 66 ## |
67 ## The second output, @var{position}, provides the position, in characters, | |
68 ## from the beginning of the file. | |
69 ## | |
70 ## @seealso{dlmread, fscanf, load, strread, textread} | |
71 ## @end deftypefn | |
72 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
73 function [C, position] = textscan (fid, format = "%f", varargin) |
11141 | 74 |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
75 BUFLENGTH = 4096; ## Read buffer |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
76 |
11141 | 77 ## Check input |
78 if (nargin < 1) | |
79 print_usage (); | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
80 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
81 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
82 if (isempty (format)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
83 format = "%f"; |
11141 | 84 endif |
85 | |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
86 if (! ischar (format)) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
87 error ("textscan: FORMAT must be a string"); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
88 endif |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
89 |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
90 ## Determine the number of data fields & initialize output array |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
91 num_fields = numel (strfind (format, "%")) - numel (strfind (format, "%*")); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
92 C = cell (1, num_fields); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
93 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
94 if (! (isa (fid, "double") && fid > 0) && ! ischar (fid)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
95 error ("textscan: first argument must be a file id or character string"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
96 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
97 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
98 args = varargin; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
99 if (nargin > 2 && isnumeric (args{1})) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
100 nlines = args{1}; |
11141 | 101 else |
102 nlines = Inf; | |
103 endif | |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
104 if (nlines < 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
105 printf ("textscan: N = 0, no data read\n"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
106 return |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
107 endif |
11141 | 108 |
109 if (! any (strcmpi (args, "emptyvalue"))) | |
110 ## Matlab returns NaNs for missing values | |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
111 args(end+1:end+2) = {'emptyvalue', NaN}; |
11141 | 112 endif |
113 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
114 ## Check default parameter values that differ for strread & textread |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
115 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
116 ipos = find (strcmpi (args, "whitespace")); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
117 if (isempty (ipos)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
118 ## Matlab default whitespace = " \b\t" |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
119 args(end+1:end+2) = {'whitespace', " \b\t"}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
120 whitespace = " \b\t"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
121 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
122 ## Check if there's at least one string format specifier |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 has_str_fmt = regexp (format, '%[*]?\d*s', "once"); |
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
124 ## If there is a string format AND whitespace value = empty, |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
125 ## don't add a space (char(32)) to whitespace |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 if (! (isempty (args{ipos+1}) && has_str_fmt)) |
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
127 args{ipos+1} = unique ([" ", args{ipos+1}]); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
128 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
129 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
130 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
131 if (! any (strcmpi (args, "delimiter"))) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
132 ## Matlab says default delimiter = whitespace. |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
133 ## strread() will pick this up further |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
134 args(end+1:end+2) = {'delimiter', ""}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
135 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
136 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
137 collop = false; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
138 ipos = find (strcmpi (args, "collectoutput")); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
139 if (! isempty (ipos)) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
140 ## Search & concatenate consecutive columns of same class requested |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
141 if (isscalar (args{ipos+1}) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
142 && (islogical (args{ipos+1}) || isnumeric (args{ipos+1}))) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
143 collop = args{ipos+1}; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
144 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
145 warning ("textscan: illegal value for CollectOutput parameter - ignored"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
146 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
147 ## Remove argument before call to strread() below |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
148 args(ipos:ipos+1) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
149 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
150 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
151 if (any (strcmpi (args, "returnonerror"))) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
152 ## Because of the way strread() reads data (columnwise) this parameter |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
153 ## can't be neatly implemented. strread() will pick it up anyway |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
154 warning ('textscan: ReturnOnError is not fully implemented'); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
155 else |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
156 ## Set default value (=true) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
157 args(end+1:end+2) = {"returnonerror", 1}; |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
158 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
159 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
160 if (ischar (fid)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
161 ## Read from a text string |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
162 if (nargout == 2) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
163 error ("textscan: cannot provide position information for character input"); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
164 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
165 str = fid; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
166 else |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
167 st_pos = ftell (fid); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
168 ## Skip header lines if requested |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
169 headerlines = find (strcmpi (args, "headerlines"), 1); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
170 ## Beware of zero valued headerline, fskipl would skip to EOF |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
171 if (! isempty (headerlines) && (args{headerlines + 1} > 0)) |
14582
e97ec01d4157
Correct assignment to wrong variable f headerlines processing
Alexander Graf <a.graf@fz-juelich.de>
parents:
14565
diff
changeset
|
172 fskipl (fid, args{headerlines + 1}); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
173 args(headerlines:headerlines+1) = []; |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
174 st_pos = ftell (fid); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
175 endif |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
176 ## Read a first file chunk. Rest follows after endofline processing |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
177 [str, count] = fscanf (fid, "%c", BUFLENGTH); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
178 endif |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
179 |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
180 ## Check for empty result |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
181 if (isempty (str)) |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
182 warning ("textscan: no data read"); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
183 return; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
184 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
185 |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
186 ## Check value of 'endofline'. String or file doesn't seem to matter |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
187 endofline = find (strcmpi (args, "endofline"), 1); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
188 if (! isempty (endofline)) |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
189 if (ischar (args{endofline + 1})) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
190 eol_char = args{endofline + 1}; |
14213
a022c04f68cc
Replace to-be-deprecated strmatch occurrences with alternate code.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 if (! any (strcmp (eol_char, {"", "\n", "\r", "\r\n"}))) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
192 error ("textscan: illegal EndOfLine character value specified"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
193 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
194 else |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
195 error ("textscan: character value required for EndOfLine"); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
196 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
197 else |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
198 ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
199 eol_srch_len = min (length (str), BUFLENGTH); |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
200 ## First try DOS (CRLF) |
14214
2fe0f5fa8cc3
Replace to-be-deprecated findstr occurrences with strfind.
Rik <octave@nomad.inbox5.com>
parents:
14213
diff
changeset
|
201 if (! isempty (strfind ("\r\n", str(1 : eol_srch_len)))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
202 eol_char = "\r\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
203 ## Perhaps old Macintosh? (CR) |
14214
2fe0f5fa8cc3
Replace to-be-deprecated findstr occurrences with strfind.
Rik <octave@nomad.inbox5.com>
parents:
14213
diff
changeset
|
204 elseif (! isempty (strfind ("\r", str(1 : eol_srch_len)))) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
205 eol_char = "\r"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
206 ## Otherwise, use plain UNIX (LF) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
207 else |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
208 eol_char = "\n"; |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
209 endif |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
210 ## Set up the default endofline param value |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
211 args(end+1:end+2) = {"endofline", eol_char}; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
212 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
213 |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
214 if (!ischar (fid)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
215 ## Now that we know what EOL looks like, we can process format_repeat_count. |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
216 ## FIXME The below isn't ML-compatible: counts lines, not format string uses |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
217 if (isfinite (nlines) && (nlines >= 0)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
218 l_eol_char = length (eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
219 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
220 n_eoi = length (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
221 nblks = 0; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
222 ## Avoid slow repeated str concatenation, first seek requested end of data |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
223 while (n_eoi < nlines && count == BUFLENGTH) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
224 [nstr, count] = fscanf (fid, "%c", BUFLENGTH); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
225 if (count > 0) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
226 ## Watch out for multichar EOL being missed across buffer boundaries |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
227 if (l_eol_char > 1) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
228 str = [str(end - length (eol_char) + 2 : end) nstr]; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
229 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
230 str = nstr; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
231 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
232 eoi = findstr (str, eol_char); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
233 n_eoi += numel (eoi); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
234 ++nblks; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
235 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
236 endwhile |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
237 ## OK, found EOL delimiting last requested line. Compute ptr (incl. EOL) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
238 if (isempty (eoi)) |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
239 printf ("textscan: format repeat count specified but no endofline found\n"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
240 data_size = nblks * BUFLENGTH + count; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
241 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
242 ## Compute data size to read incl complete EOL |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
243 data_size = (nblks * BUFLENGTH) + eoi(end + min (nlines, n_eoi) - n_eoi) \ |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
244 + l_eol_char - 1; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
245 endif |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
246 fseek (fid, st_pos, "bof"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
247 str = fscanf (fid, "%c", data_size); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
248 else |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
249 fseek (fid, st_pos, "bof"); |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
250 str = fread (fid, "char=>char").'; |
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
251 endif |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
252 endif |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
253 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
254 ## Determine the number of data fields |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
255 num_fields = numel (strfind (format, "%")) - numel (strfind (format, "%*")); |
11141 | 256 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
257 ## Strip trailing EOL to avoid returning stray missing values (f. strread). |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
258 ## However, in case of CollectOutput request, presence of EOL is required |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
259 eol_at_end = strcmp (str(end-length (eol_char) + 1 : end), eol_char); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
260 if (collop) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
261 if (! eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
262 str(end+1 : end+length (eol_char)) = eol_char; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
263 endif |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
264 elseif (eol_at_end) |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
265 str(end-length (eol_char) + 1 : end) = ""; |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
266 ## A corner case: str may now be empty.... |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
267 if (isempty (str)); return; endif |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
268 endif |
11141 | 269 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
270 ## Call strread to make it do the real work |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
271 C = cell (1, num_fields); |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
272 [C{:}] = strread (str, format, args{:}); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12916
diff
changeset
|
273 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
274 ## If requested, collect output columns of same class |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
275 if (collop) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
276 C = colloutp (C); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
277 endif |
11141 | 278 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
279 if (nargout == 2) |
14565
98aaebc56d7c
2012-03-25 Philip Nienhuis <prnienhuis@users.sf.net>
Philip Nienhuis <prnienhuis@@users.sf.net>
parents:
14552
diff
changeset
|
280 ## Remember file position (persistent var) |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
281 position = ftell (fid); |
11141 | 282 endif |
283 | |
284 endfunction | |
285 | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
286 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
287 ## Collect consecutive columns of same class into one cell column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
288 function C = colloutp (C) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
289 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
290 ## Start at rightmost column and work backwards to avoid ptr mixup |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
291 ii = numel (C); |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
292 while (ii > 1) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
293 clss1 = class (C{ii}); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
294 jj = ii; |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
295 while (jj > 1 && strcmp (clss1, class (C{jj - 1}))) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
296 ## Column to the left is still same class; check next column to the left |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
297 --jj; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
298 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
299 if (jj < ii) |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
300 ## Concatenate columns into current column |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
301 C{jj} = [C{jj : ii}]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
302 ## Wipe concatenated columns to the right, resume search to the left |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
303 C(jj+1 : ii) = []; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
304 ii = jj - 1; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
305 else |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
306 ## No similar class in column to the left, search from there |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
307 --ii; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
308 endif |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
309 endwhile |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
310 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
311 endfunction |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
312 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
313 |
11141 | 314 %!test |
315 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12"; | |
316 %! fmtstr = "%f %d %f %s"; | |
317 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf); | |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
318 %! assert (isequal (c{1}, [1;5])); |
11141 | 319 %! assert (length (c{1}), 2); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
320 %! assert (iscellstr (c{4})); |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
321 %! assert (isequal (c{3}, [3; -Inf])); |
11141 | 322 |
323 %!test | |
324 %! b = [10:10:100]; | |
325 %! b = [b; 8*b/5]; | |
326 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b); | |
327 %! fmt = "%f miles/hr = %f kilometers/hr"; | |
328 %! c = textscan (str, fmt); | |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
329 %! assert (b(1,:)', c{1}, 1e-5); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
330 %! assert (b(2,:)', c{2}, 1e-5); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
331 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
332 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
333 %! str = "13, 72, NA, str1, 25\r\n// Middle line\r\n36, na, 05, str3, 6"; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
334 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",","treatAsEmpty", {"NA", "na"},"commentStyle", "//"); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
335 %! assert (a{1}, int32 ([13; 36])); |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
336 %! assert (a{2}, [72; NaN]); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
337 %! assert (a{3}, [NaN; 5]); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
338 %! assert (a{4}, {"str1"; "str3"}); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
339 %! assert (a{5}, [25; 6]); |
11141 | 340 |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
341 %!test |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
342 %! str = "Km:10 = hhhBjjj miles16hour\r\n"; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
343 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
344 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
345 %! str = [str "Km:25 = hhhZ\r\n"]; |
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
346 %! fmt = "Km:%d = hhh%1sjjj miles%dhour"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
347 %! a = textscan (str, fmt, "delimiter", " "); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
348 %! assert (a{1}', int32 ([10 15 2 25])); |
12866
fe6e2afcd9ee
Revamp strread, textscan, textread functions for Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12679
diff
changeset
|
349 %! assert (a{2}', {'B' 'J' 'R' 'Z'}); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
350 %! assert (a{3}', int32 ([16 241 3 0])); |
11141 | 351 |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
352 %% Test with default endofline parameter |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
353 %!test |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
354 %! c = textscan ("L1\nL2", "%s"); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
355 %! assert (c{:}, {"L1"; "L2"}); |
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
356 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
357 %% Test with endofline parameter set to "" (empty) - newline should be in word |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
358 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
359 %! c = textscan ("L1\nL2", "%s", "endofline", ""); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
360 %! assert (int8 (c{:}{:}), int8 ([ 76, 49, 10, 76, 50 ])); |
12891
f5a3f77d51aa
strread.m, textscan.m: More Matlab compatability
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12880
diff
changeset
|
361 |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
362 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
363 %! # No delimiters at all besides EOL. Skip fields, even empty fields |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
364 %! str = "Text1Text2Text\nTextText4Text\nText57Text"; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
365 %! c = textscan (str, "Text%*dText%dText"); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
366 %! assert (c{1}, int32 ([2; 4; 0])); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
367 |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
368 %!test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
369 %% CollectOutput test |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
370 %! b = [10:10:100]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
371 %! b = [b; 8*b/5; 8*b*1000/5]; |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
372 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b); |
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
373 %! fmt = "%f miles%s %s %f (%f) kilometers %*s"; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14214
diff
changeset
|
374 %! c = textscan (str, fmt, "collectoutput", 1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
375 %! assert (size (c{3}), [10, 2]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
376 %! assert (size (c{2}), [10, 2]); |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
377 |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
378 %!test |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
379 %% CollectOutput test with uneven column length files |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
380 %! b = [10:10:100]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
381 %! b = [b; 8*b/5; 8*b*1000/5]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
382 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
383 %! str = [str "110 miles/hr"]; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
384 %! fmt = "%f miles%s %s %f (%f) kilometers %*s"; |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
385 %! c = textscan (str, fmt, "collectoutput", 1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
386 %! assert (size (c{1}), [11, 1]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
387 %! assert (size (c{3}), [11, 2]); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
388 %! assert (size (c{2}), [11, 2]); |
14799
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
389 %! assert (c{3}(end), NaN); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
390 %! assert (c{2}{11, 1}, "/hr"); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
391 %! assert (isempty (c{2}{11, 2}), true); |
21197c43f984
Better Matlab compatibility of textscan, bugs #36356 and #36392
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14621
diff
changeset
|
392 |
12877
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
393 %% Test input validation |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
394 %!error textscan () |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
395 %!error textscan (single (4)) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
396 %!error textscan ({4}) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
397 %!error <must be a string> textscan ("Hello World", 2) |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
398 %!error <cannot provide position information> [C, pos] = textscan ("Hello World") |
ddea3962b024
Various improvements to strread, textread, textscan functions
Rik <octave@nomad.inbox5.com>
parents:
12873
diff
changeset
|
399 %!error <character value required> textscan ("Hello World", '%s', 'EndOfLine', 3) |
12914
ac00ae83ea2e
More compatibility improvements for textscan and strread
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
12891
diff
changeset
|
400 |
14800
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
401 %! Test incomplete first data line |
a922f768ee09
textscan, strread: improved ML compatibility (bug 36398)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
14799
diff
changeset
|
402 %! R = textscan (['Empty1' char(10)], 'Empty%d %f'); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
403 %! assert (R{1}, int32 (1)); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14853
diff
changeset
|
404 %! assert (isempty (R{2}), true); |