]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/tblwrite.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / tblwrite.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} {} tblwrite (@var{data}, @var{varnames}, @var{casenames}, @var{filename})
18 ## @deftypefnx {Function File} {} tblwrite (@var{data}, @var{varnames}, @var{casenames}, @var{filename}, @var{delimeter})
19 ## Write tabular data to an ascii file.
20 ##
21 ## @var{data} is written to 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 written starting at cell (2,2) where the
33 ## @var{varnames} are a char matrix or cell vector written to the first
34 ## row (starting at (1,2)), and the @var{casenames} are a char matrix
35 ## (or cell vector) written to the first column (starting at (2,1)).
36 ## @seealso{tblread, csv2cell, cell2csv}
37 ## @end deftypefn
38
39 function tblwrite (data, varnames, casenames, f="", d=" ")
40
41   ## Check arguments
42   if nargin < 4 || nargin > 5
43     print_usage ();
44   endif
45   varnames = __makecell__ (varnames, "varnames");
46   casenames = __makecell__ (casenames, "varnames");
47   if numel (varnames) != columns (data)
48     error ("tblwrite: the number of rows (or cells) in varnames must equal the number of columns in data")
49   endif
50   if numel (varnames) != rows (data)
51     error ("tblwrite: the number of rows (or cells) in casenames must equal the number of rows in data")
52   endif
53
54   if isempty (f)
55     ## FIXME: open a file dialog box in this case when a file dialog box
56     ## becomes available
57     error ("tblread: filename must be given")
58   endif
59   [d err] = tbl_delim (d);
60   if ! isempty (err)
61     error ("tblwrite: %s", err)
62   endif
63
64   dat = cell (size (data) + 1);
65   dat(1,2:end) = varnames;
66   dat(2:end,1) = casenames;
67   dat(2:end,2:end) = mat2cell (data,
68                                ones (rows (data), 1),
69                                ones (columns (data), 1));;
70   cell2csv (f, dat, d);
71
72 endfunction
73
74 function x = __makecell__ (x, name)
75   ## force x into a cell matrix
76   if ! iscell (x)
77     if ischar (x)
78       ## convert varnames into a cell
79       x = mat2cell (x, ones (rows (x), 1));
80     else
81       error ("tblwrite: %s must be either a char or a cell", name)
82     endif
83   endif
84 endfunction
85
86 ## Tests
87 %!shared d, v, c
88 %! d = [1 2;3 4];
89 %! v = ["a ";"bc"];
90 %! c = ["de";"f "];
91 %!test
92 %! tblwrite (d, v, c, "tblwrite-space.dat");
93 %! [dt vt ct] = tblread ("tblwrite-space.dat", " ");
94 %! assert (dt, d);
95 %! assert (vt, v);
96 %! assert (ct, c);
97 %!test
98 %! tblwrite (d, v, c, "tblwrite-space.dat", " ");
99 %! [dt vt ct] = tblread ("tblwrite-space.dat", " ");
100 %! assert (dt, d);
101 %! assert (vt, v);
102 %! assert (ct, c);
103 %!test
104 %! tblwrite (d, v, c, "tblwrite-space.dat", "space");
105 %! [dt vt ct] = tblread ("tblwrite-space.dat");
106 %! assert (dt, d);
107 %! assert (vt, v);
108 %! assert (ct, c);
109 %!test
110 %! tblwrite (d, v, c, "tblwrite-tab.dat", "tab");
111 %! [dt vt ct] = tblread ("tblwrite-tab.dat", "tab");
112 %! assert (dt, d);
113 %! assert (vt, v);
114 %! assert (ct, c);
115 %!test
116 %! tblwrite (d, v, c, "tblwrite-tab.dat", "\t");
117 %! [dt vt ct] = tblread ("tblwrite-tab.dat", "\t");
118 %! assert (dt, d);
119 %! assert (vt, v);
120 %! assert (ct, c);
121 %!test
122 %! tblwrite (d, v, c, "tblwrite-tab.dat", '\t');
123 %! [dt vt ct] = tblread ("tblwrite-tab.dat", '\t');
124 %! assert (dt, d);
125 %! assert (vt, v);
126 %! assert (ct, c);