From: <ws...@us...> - 2008-05-18 15:36:33
|
Revision: 5066 http://octave.svn.sourceforge.net/octave/?rev=5066&view=rev Author: wsloand Date: 2008-05-18 08:36:23 -0700 (Sun, 18 May 2008) Log Message: ----------- caseread.m, casewrite.m: new functions caseread.dat: test file for caseread Added Paths: ----------- trunk/octave-forge/main/statistics/inst/caseread.m trunk/octave-forge/main/statistics/inst/casewrite.m trunk/octave-forge/main/statistics/test/caseread.dat Added: trunk/octave-forge/main/statistics/inst/caseread.m =================================================================== --- trunk/octave-forge/main/statistics/inst/caseread.m (rev 0) +++ trunk/octave-forge/main/statistics/inst/caseread.m 2008-05-18 15:36:23 UTC (rev 5066) @@ -0,0 +1,61 @@ +## Copyright (C) 2008 Bill Denney +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{names} =} caseread (@var{filename}) +## Read case names from an ascii file. +## +## Essentially, this reads all lines from a file as text and returns +## them in a string matrix. +## @seealso{casewrite, tblread, tblwrite, csv2cell, cell2csv, fopen} +## @end deftypefn + +## Author: Bill Denney <bi...@de...> +## Description: Read strings from a file + +function names = caseread (f="") + + ## Check arguments + if nargin != 1 + print_usage (); + endif + if isempty (f) + ## FIXME: open a file dialog box in this case when a file dialog box + ## becomes available + error ("caseread: filename must be given") + endif + + [fid msg] = fopen (f, "rt"); + if fid < 0 || (! isempty (msg)) + error ("caseread: cannot open %s: %s", f, msg); + endif + + names = {}; + t = fgetl (fid); + while ischar (t) + names{end+1} = t; + t = fgetl (fid); + endwhile + if (fclose (fid) < 0) + error ("caseread: error closing f") + endif + names = strvcat (names); + +endfunction + +## Tests +%!shared n +%! n = ["a ";"bcd";"ef "]; +%!assert (caseread ("caseread.dat"), n); Added: trunk/octave-forge/main/statistics/inst/casewrite.m =================================================================== --- trunk/octave-forge/main/statistics/inst/casewrite.m (rev 0) +++ trunk/octave-forge/main/statistics/inst/casewrite.m 2008-05-18 15:36:23 UTC (rev 5066) @@ -0,0 +1,66 @@ +## Copyright (C) 2008 Bill Denney +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} casewrite (@var{strmat}, @var{filename}) +## Write case names to an ascii file. +## +## Essentially, this writes all lines from @var{strmat} to +## @var{filename} (after deblanking them). +## @seealso{caseread, tblread, tblwrite, csv2cell, cell2csv, fopen} +## @end deftypefn + +## Author: Bill Denney <bi...@de...> +## Description: Write strings from a file + +function names = caseread (s="", f="") + + ## Check arguments + if nargin != 2 + print_usage (); + endif + if isempty (f) + ## FIXME: open a file dialog box in this case when a file dialog box + ## becomes available + error ("casewrite: filename must be given") + endif + if isempty (s) + error ("casewrite: strmat must be given") + elseif ! ischar (s) + error ("casewrite: strmat must be a character matrix") + elseif ndims (s) != 2 + error ("casewrite: strmat must be two dimensional") + endif + + [fid msg] = fopen (f, "wt"); + if fid < 0 || (! isempty (msg)) + error ("casewrite: cannot open %s for writing: %s", f, msg); + endif + + for i = 1:rows (s) + status = fputs (fid, sprintf ("%s\n", deblank (s(i,:)))); + endfor + if (fclose (fid) < 0) + error ("casewrite: error closing f") + endif + +endfunction + +## Tests +%!shared s +%! s = ["a ";"bcd";"ef "]; +%!test +%! casewrite (s, "casewrite.dat") +%! assert(caseread ("casewrite.dat"), s); Added: trunk/octave-forge/main/statistics/test/caseread.dat =================================================================== --- trunk/octave-forge/main/statistics/test/caseread.dat (rev 0) +++ trunk/octave-forge/main/statistics/test/caseread.dat 2008-05-18 15:36:23 UTC (rev 5066) @@ -0,0 +1,3 @@ +a +bcd +ef \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |