1 ## Copyright (C) 2002-2012 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{M})
21 ## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{M}, @var{delim}, @var{r}, @var{c})
22 ## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{M}, @var{key}, @var{val} @dots{})
23 ## @deftypefnx {Function File} {} dlmwrite (@var{file}, @var{M}, "-append", @dots{})
24 ## @deftypefnx {Function File} {} dlmwrite (@var{fid}, @dots{})
25 ## Write the matrix @var{M} to the named file using delimiters.
27 ## @var{file} should be a file name or writable file ID given by @code{fopen}.
29 ## The parameter @var{delim} specifies the delimiter to use to separate
32 ## The value of @var{r} specifies the number of delimiter-only lines to
33 ## add to the start of the file.
35 ## The value of @var{c} specifies the number of delimiters to prepend to
38 ## If the argument @code{"-append"} is given, append to the end of
41 ## In addition, the following keyword value pairs may appear at the end
42 ## of the argument list:
46 ## Either @samp{"on"} or @samp{"off"}. See @samp{"-append"} above.
49 ## See @var{delim} above.
52 ## The character(s) to use to separate each row. Three special cases
53 ## exist for this option. @samp{"unix"} is changed into "\n",
54 ## @samp{"pc"} is changed into "\r\n", and @samp{"mac"} is changed
55 ## into "\r". Other values for this option are kept as is.
64 ## The precision to use when writing the file. It can either be a
65 ## format string (as used by fprintf) or a number of significant digits.
69 ## dlmwrite ("file.csv", reshape (1:16, 4, 4));
73 ## dlmwrite ("file.tex", a, "delimiter", "&", "newline", "\\n")
76 ## @seealso{dlmread, csvread, csvwrite}
79 ## Author: Paul Kienzle <pkienzle@users.sf.net>
81 ## This program was originally granted to the public domain
83 ## 2002-03-08 Paul Kienzle <pkienzle@users.sf.net>
85 ## 2005-11-27 Bill Denney <bill@givebillmoney.com>
86 ## * Significant modifications of the input arguments for additional
89 function dlmwrite (file, M, varargin)
106 ## process the input arguments
108 while (i < length (varargin))
110 if (strcmpi (varargin{i}, "delimiter"))
111 delim = varargin{++i};
112 elseif (strcmpi (varargin{i}, "newline"))
113 newline = varargin{++i};
114 if (strcmpi (newline, "unix"))
116 elseif (strcmpi (newline, "pc"))
118 elseif (strcmpi (newline, "mac"))
121 elseif (strcmpi (varargin{i}, "roffset"))
123 elseif (strcmpi (varargin{i}, "coffset"))
125 elseif (strcmpi (varargin{i}, "precision"))
126 precision = varargin{++i};
127 if (! strcmpi (class (precision), "char"))
128 precision = sprintf ("%%.%gg", precision);
130 elseif (strcmpi (varargin{i}, "-append"))
132 elseif (strcmpi (varargin{i}, "append"))
134 if (strcmpi (varargin{i}, "on"))
136 elseif (strcmpi (varargin{i}, "off"))
139 error ('dlmwrite: append must be "on" or "off"');
155 [fid, msg] = fopen (file, opentype);
156 elseif (isscalar (file) && isnumeric (file))
157 [fid, msg] = deal (file, "invalid file number");
159 error ("dlmwrite: FILE must be a filename string or numeric FID");
163 error (["dlmwrite: " msg]);
167 repmat ([repmat(delim, 1, c + columns(M)-1), newline], 1, r));
170 cprecision = regexprep (precision, '^%([-\d.])', '%+$1');
171 template = [precision, cprecision, "i", ...
172 repmat([delim, precision, cprecision, "i"], 1, ...
173 columns(M) - 1), newline ];
175 template = [precision, repmat([delim, precision], 1, columns(M)-1),...
179 template = [repmat(delim, 1, c), template];
183 b = zeros (2*rows(M), columns (M));
184 b(1: 2 : end, :) = real (M);
185 b(2: 2 : end, :) = imag (M);
186 fprintf (fid, template, b);
188 fprintf (fid, template, M.');
190 if (! isscalar (file))
200 %! dlmwrite (f,[1,2;3,4],'precision','%5.2f','newline','unix','roffset',1,'coffset',1);
201 %! fid = fopen (f,"rt");
202 %! f1 = char (fread (fid,Inf,'char')');
204 %! dlmwrite (f,[5,6],'precision','%5.2f','newline','unix','coffset',1,'delimiter',',','-append');
205 %! fid = fopen (f,"rt");
206 %! f2 = char (fread (fid,Inf,'char')');
210 %! assert (f1,",,\n, 1.00, 2.00\n, 3.00, 4.00\n");
211 %! assert (f2,",,\n, 1.00, 2.00\n, 3.00, 4.00\n, 5.00, 6.00\n");