changelog shortlog tags changeset files revisions annotate raw

scripts/io/dlmwrite.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (70 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2002, 2008, 2009 Paul Kienzle
2##
3## This file is part of Octave.
4##
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.
9##
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.
14##
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/>.
18
19## -*- texinfo -*-
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.
25##
26## The parameter @var{delim} specifies the delimiter to use to separate
27## values on a row.
28##
29## The value of @var{r} specifies the number of delimiter-only lines to
30## add to the start of the file.
31##
32## The value of @var{c} specifies the number of delimiters to prepend to
33## each line of data.
34##
35## If the argument @code{"-append"} is given, append to the end of the
36## @var{file}.
37##
38## In addition, the following keyword value pairs may appear at the end
39## of the argument list:
40## @table @code
41## @item "append"
42## Either @samp{"on"} or @samp{"off"}. See @samp{"-append"} above.
43##
44## @item "delimiter"
45## See @var{delim} above.
46##
47## @item "newline"
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.
52##
53## @item "roffset"
54## See @var{r} above.
55##
56## @item "coffset"
57## See @var{c} above.
58##
59## @item "precision"
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.
62## @end table
63##
64## @example
65## dlmwrite ("file.csv", reshape (1:16, 4, 4));
66## @end example
67##
68## @example
69## dlmwrite ("file.tex", a, "delimiter", "&", "newline", "\\n")
70## @end example
71##
72## @seealso{dlmread, csvread, csvwrite}
73## @end deftypefn
74
75## Author: Paul Kienzle <pkienzle@users.sf.net>
76##
77## This program was originally granted to the public domain
78##
79## 2002-03-08 Paul Kienzle <pkienzle@users.sf.net>
80## * Initial revision
81## 2005-11-27 Bill Denney <bill@givebillmoney.com>
82## * Significant modifications of the input arguements for additional
83## functionality.
84
85function dlmwrite (file, a, varargin)
86
87 if (nargin < 2 || ! ischar (file))
88 print_usage ();
89 endif
90
91 ## set defaults
92 delim = ",";
93 r = 0;
94 c = 0;
95 newline = "\n";
96 if (ischar (a))
97 precision = "%c";
98 else
99 precision = "%.16g";
100 endif
101 opentype = "wt";
102
103 ## process the input arguements
104 i = 0;
105 while (i < length (varargin))
106 i = i + 1;
107 if (strcmpi (varargin{i}, "delimiter"))
108 i = i + 1;
109 delim = varargin{i};
110 elseif (strcmpi (varargin{i}, "newline"))
111 i = i + 1;
112 newline = varargin{i};
113 if (strcmpi (newline, "unix"))
114 newline = "\n";
115 elseif (strcmpi (newline, "pc"))
116 newline = "\r\n";
117 elseif (strcmpi (newline, "mac"))
118 newline = "\r";
119 endif
120 elseif (strcmpi (varargin{i}, "roffset"))
121 i = i + 1;
122 r = varargin{i};
123 elseif (strcmpi (varargin{i}, "coffset"))
124 i = i + 1;
125 c = varargin{i};
126 elseif (strcmpi (varargin{i}, "precision"))
127 i = i + 1;
128 precision = varargin{i};
129 if (! strcmpi (class (precision), "char"))
130 precision = sprintf ("%.%gg", precision);
131 endif
132 elseif (strcmpi (varargin{i}, "-append"))
133 opentype = "at";
134 elseif (strcmpi (varargin{i}, "append"))
135 i = i + 1;
136 if (strcmpi (varargin{i}, "on"))
137 opentype = "at";
138 elseif (strcmpi (varargin{i}, "off"))
139 opentype = "wt";
140 else
141 error ("dlmwrite: append must be \"on\" or \"off\".");
142 endif
143 else
144 if (i == 1)
145 delim = varargin{i};
146 elseif (i == 2)
147 r = varargin{i};
148 elseif (i == 3)
149 c = varargin{i};
150 else
151 print_usage();
152 endif
153 endif
154 endwhile
155
156 [fid, msg] = fopen (file, opentype);
157 if (fid < 0)
158 error (msg);
159 else
160 if (r > 0)
161 fprintf (fid, "%s",
162 repmat ([repmat(delim, 1, c + columns(a)-1), newline], 1, r));
163 endif
164 if (iscomplex (a))
165 cprecision = regexprep (precision, '^%([-\d.])','%+$1');
166 template = [precision, cprecision, "i", ...
167 repmat([delim, precision, cprecision, "i"], 1, ...
168 columns(a) - 1), newline ];
169 else
170 template = [precision, repmat([delim, precision], 1, columns(a)-1),...
171 newline];
172 endif
173 if (c > 0)
174 template = [repmat(delim, 1, c), template];
175 endif
176 if (iscomplex (a))
177 a = a.';
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);
182 else
183 fprintf (fid, template, a.');
184 endif
185 fclose (fid);
186 endif
187endfunction
188
189%!test
190%! f = tmpnam();
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')');
194%! fclose(fid);
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')');
198%! fclose(fid);
199%! unlink(f);
200%!
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");