]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/tblread.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / tblread.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[@var{data}, @var{varnames}, @var{casenames}] =} tblread (@var{filename})
18 ## @deftypefnx {Function File} {[@var{data}, @var{varnames}, @var{casenames}] =} tblread (@var{filename}, @var{delimeter})
19 ## Read tabular data from an ascii file.
20 ##
21 ## @var{data} is read from an ascii data file named @var{filename} with
22 ## an optional @var{delimeter}.  The delimeter may be any single
23 ## character or
24 ## @itemize
25 ## @item "space" " " (default)
26 ## @item "tab" "\t"
27 ## @item "comma" ","
28 ## @item "semi" ";"
29 ## @item "bar" "|"
30 ## @end itemize
31 ##
32 ## The @var{data} is read starting at cell (2,2) where the
33 ## @var{varnames} form a char matrix from the first row (starting at
34 ## (1,2)) vertically concatenated, and the @var{casenames} form a char
35 ## matrix read from the first column (starting at (2,1)) vertically
36 ## concatenated.
37 ## @seealso{tblwrite, csv2cell, cell2csv}
38 ## @end deftypefn
39
40 function [data, varnames, casenames] = tblread (f="", d=" ")
41
42   ## Check arguments
43   if nargin < 1 || nargin > 2
44     print_usage ();
45   endif
46   if isempty (f)
47     ## FIXME: open a file dialog box in this case when a file dialog box
48     ## becomes available
49     error ("tblread: filename must be given")
50   endif
51   [d err] = tbl_delim (d);
52   if ! isempty (err)
53     error ("tblread: %s", err)
54   endif
55
56   d = csv2cell (f, d);
57   data = cell2mat (d(2:end, 2:end));
58   varnames = strvcat (d(1,2:end));
59   casenames = strvcat (d(2:end,1));
60
61 endfunction
62
63 ## Tests
64 %!shared d, v, c
65 %! d = [1 2;3 4];
66 %! v = ["a ";"bc"];
67 %! c = ["de";"f "];
68 %!test
69 %! [dt vt ct] = tblread ("tblread-space.dat");
70 %! assert (dt, d);
71 %! assert (vt, v);
72 %! assert (ct, c);
73 %!test
74 %! [dt vt ct] = tblread ("tblread-space.dat", " ");
75 %! assert (dt, d);
76 %! assert (vt, v);
77 %! assert (ct, c);
78 %!test
79 %! [dt vt ct] = tblread ("tblread-space.dat", "space");
80 %! assert (dt, d);
81 %! assert (vt, v);
82 %! assert (ct, c);
83 %!test
84 %! [dt vt ct] = tblread ("tblread-tab.dat", "tab");
85 %! assert (dt, d);
86 %! assert (vt, v);
87 %! assert (ct, c);
88 %!test
89 %! [dt vt ct] = tblread ("tblread-tab.dat", "\t");
90 %! assert (dt, d);
91 %! assert (vt, v);
92 %! assert (ct, c);
93 %!test
94 %! [dt vt ct] = tblread ("tblread-tab.dat", '\t');
95 %! assert (dt, d);
96 %! assert (vt, v);
97 %! assert (ct, c);