1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
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
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
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/>.
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.
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
25 ## @item "space" " " (default)
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}
39 function tblwrite (data, varnames, casenames, f="", d=" ")
42 if nargin < 4 || nargin > 5
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")
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")
55 ## FIXME: open a file dialog box in this case when a file dialog box
57 error ("tblread: filename must be given")
59 [d err] = tbl_delim (d);
61 error ("tblwrite: %s", err)
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));;
74 function x = __makecell__ (x, name)
75 ## force x into a cell matrix
78 ## convert varnames into a cell
79 x = mat2cell (x, ones (rows (x), 1));
81 error ("tblwrite: %s must be either a char or a cell", name)
92 %! tblwrite (d, v, c, "tblwrite-space.dat");
93 %! [dt vt ct] = tblread ("tblwrite-space.dat", " ");
98 %! tblwrite (d, v, c, "tblwrite-space.dat", " ");
99 %! [dt vt ct] = tblread ("tblwrite-space.dat", " ");
104 %! tblwrite (d, v, c, "tblwrite-space.dat", "space");
105 %! [dt vt ct] = tblread ("tblwrite-space.dat");
110 %! tblwrite (d, v, c, "tblwrite-tab.dat", "tab");
111 %! [dt vt ct] = tblread ("tblwrite-tab.dat", "tab");
116 %! tblwrite (d, v, c, "tblwrite-tab.dat", "\t");
117 %! [dt vt ct] = tblread ("tblwrite-tab.dat", "\t");
122 %! tblwrite (d, v, c, "tblwrite-tab.dat", '\t');
123 %! [dt vt ct] = tblread ("tblwrite-tab.dat", '\t');