]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/casewrite.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / casewrite.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} {} casewrite (@var{strmat}, @var{filename})
18 ## Write case names to an ascii file.
19 ##
20 ## Essentially, this writes all lines from @var{strmat} to
21 ## @var{filename} (after deblanking them).
22 ## @seealso{caseread, tblread, tblwrite, csv2cell, cell2csv, fopen}
23 ## @end deftypefn
24
25 ## Author: Bill Denney <bill@denney.ws>
26 ## Description: Write strings from a file
27
28 function names = casewrite (s="", f="")
29
30   ## Check arguments
31   if nargin != 2
32     print_usage ();
33   endif
34   if isempty (f)
35     ## FIXME: open a file dialog box in this case when a file dialog box
36     ## becomes available
37     error ("casewrite: filename must be given")
38   endif
39   if isempty (s)
40     error ("casewrite: strmat must be given")
41   elseif ! ischar (s)
42     error ("casewrite: strmat must be a character matrix")
43   elseif ndims (s) != 2
44     error ("casewrite: strmat must be two dimensional")
45   endif
46
47   [fid msg] = fopen (f, "wt");
48   if fid < 0 || (! isempty (msg))
49     error ("casewrite: cannot open %s for writing: %s", f, msg);
50   endif
51
52   for i = 1:rows (s)
53     status = fputs (fid, sprintf ("%s\n", deblank (s(i,:))));
54   endfor
55   if (fclose (fid) < 0)
56     error ("casewrite: error closing f")
57   endif
58
59 endfunction
60
61 ## Tests
62 %!shared s
63 %! s = ["a  ";"bcd";"ef "];
64 %!test
65 %! casewrite (s, "casewrite.dat")
66 %! assert(caseread ("casewrite.dat"), s);