comparison scripts/io/textread.m @ 9746:31a22d48f41f

scripts/io/strread.m scripts/io/textread.m: new functions
author Soren Hauberg <hauberg@gmail.com>
date Mon, 19 Oct 2009 20:19:40 +0200
parents
children 09da0bd91412
comparison
equal deleted inserted replaced
9745:30d62079c493 9746:31a22d48f41f
1 ## Copyright (C) 2009 Eric Chassande-Mottin, CNRS (France)
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {[@var{a}, @dots{}] =} textread (@var{filename})
19 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format})
20 ## @deftypefnx {Function File} {[@var{a}, @dots{}] =} textread (@var{filename}, @var{format}, @
21 ## @var{prop1}, @var{value1}, @dots{})
22 ## Read data from a text file.
23 ##
24 ## The file @var{filename} is read and parsed according to @var{format}. The
25 ## function behaves like @code{strread} except it works by parsing a file instead
26 ## of a string. See the documentation of @code{strread} for details.
27 ## In addition to the options supported by @code{strread}, this function
28 ## supports one more:
29 ## @itemize
30 ## @item "headerlines":
31 ## @end itemize
32 ## The first @var{value} number of lines of @var{str} are skipped.
33 ## @seealso{strread, load, dlmread, fscanf}
34 ## @end deftypefn
35
36 function varargout = textread (filename, formatstr = "%f", varargin)
37 ## Check input
38 if (nargin < 1)
39 print_usage ();
40 endif
41
42 if (!ischar (filename) || !ischar (filename))
43 error ("textread: first and second input arguments must be strings");
44 endif
45
46 ## Read file
47 fid = fopen (filename, "r");
48 if (fid == -1)
49 error ("textread: could not open '%s' for reading", filename);
50 endif
51
52 ## Maybe skip header lines
53 headerlines = find (strncmp (varargin, "headerlines"), 1);
54 if (! isempty (headerlines))
55 fskipl (fid, headerlines);
56 varargin(headerlines:headerlines+1) = [];
57 endif
58
59 str = fread (fid, "char=>char").';
60 fclose (fid);
61
62 ## Call strread to make it do the real work
63 [varargout{1:max (nargout, 1)}] = strread (str, formatstr, varargin {:});
64 endfunction