1## Copyright (C) 2002, 2008, 2009 Paul Kienzle
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {} dlmwrite (@var{file}, @var{a})
21## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{a}, @var{delim}, @var{r}, @var{c})
22## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{a}, @var{key}, @var{val} @dots{})
23## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{a}, "-append", @dots{})
24## Write the matrix @var{a} to the named file using delimiters.
26## The parameter @var{delim} specifies the delimiter to use to separate
29## The value of @var{r} specifies the number of delimiter-only lines to
30## add to the start of the file.
32## The value of @var{c} specifies the number of delimiters to prepend to
35## If the argument @code{"-append"} is given, append to the end of the
38## In addition, the following keyword value pairs may appear at the end
39## of the argument list:
42## Either @samp{"on"} or @samp{"off"}. See @samp{"-append"} above.
45## See @var{delim} above.
48## The character(s) to use to separate each row. Three special cases
49## exist for this option. @samp{"unix"} is changed into '\n',
50## @samp{"pc"} is changed into '\r\n', and @samp{"mac"} is changed
51## into '\r'. Other values for this option are kept as is.
60## The precision to use when writing the file. It can either be a
61## format string (as used by fprintf) or a number of significant digits.
65## dlmwrite ("file.csv", reshape (1:16, 4, 4));
69## dlmwrite ("file.tex", a, "delimiter", "&", "newline", "\\n")
72## @seealso{dlmread, csvread, csvwrite}
75## Author: Paul Kienzle <pkienzle@users.sf.net>
77## This program was originally granted to the public domain
79## 2002-03-08 Paul Kienzle <pkienzle@users.sf.net>
81## 2005-11-27 Bill Denney <bill@givebillmoney.com>
82## * Significant modifications of the input arguements for additional
85function dlmwrite (file, a, varargin)
87 if (nargin < 2 || ! ischar (file))
103 ## process the input arguements
105 while (i < length (varargin))
107 if (strcmpi (varargin{i}, "delimiter"))
110 elseif (strcmpi (varargin{i}, "newline"))
112 newline = varargin{i};
113 if (strcmpi (newline, "unix"))
115 elseif (strcmpi (newline, "pc"))
117 elseif (strcmpi (newline, "mac"))
120 elseif (strcmpi (varargin{i}, "roffset"))
123 elseif (strcmpi (varargin{i}, "coffset"))
126 elseif (strcmpi (varargin{i}, "precision"))
128 precision = varargin{i};
129 if (! strcmpi (class (precision), "char"))
130 precision = sprintf ("%.%gg", precision);
132 elseif (strcmpi (varargin{i}, "-append"))
134 elseif (strcmpi (varargin{i}, "append"))
136 if (strcmpi (varargin{i}, "on"))
138 elseif (strcmpi (varargin{i}, "off"))
141 error ("dlmwrite: append must be \"on\" or \"off\".");
156 [fid, msg] = fopen (file, opentype);
162 repmat ([repmat(delim, 1, c + columns(a)-1), newline], 1, r));
165 cprecision = regexprep (precision, '^%([-\d.])','%+$1');
166 template = [precision, cprecision, "i", ...
167 repmat([delim, precision, cprecision, "i"], 1, ...
168 columns(a) - 1), newline ];
170 template = [precision, repmat([delim, precision], 1, columns(a)-1),...
174 template = [repmat(delim, 1, c), template];
178 b = zeros (2*rows(a), columns (a));
179 b(1: 2 : end, :) = real (a);
180 b(2: 2 : end, :) = imag (a);
181 fprintf (fid, template, b);
183 fprintf (fid, template, a.');
191%! dlmwrite(f,[1,2;3,4],'precision','%5.2f','newline','unix','roffset',1,'coffset',1);
192%! fid = fopen(f,"rt");
193%! f1 = char(fread(fid,Inf,'char')');
195%! dlmwrite(f,[5,6],'precision','%5.2f','newline','unix','coffset',1,'delimiter',',','-append');
196%! fid = fopen(f,"rt");
197%! f2 = char(fread(fid,Inf,'char')');
201%! assert(f1,",,\n, 1.00, 2.00\n, 3.00, 4.00\n");
202%! assert(f2,",,\n, 1.00, 2.00\n, 3.00, 4.00\n, 5.00, 6.00\n");