[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: disp() to a file?
From: |
John W. Eaton |
Subject: |
Re: disp() to a file? |
Date: |
Wed, 28 Jun 2000 14:23:47 -0500 (CDT) |
On 27-Jun-2000, Paul Kienzle <address@hidden> wrote:
| From: "John W. Eaton" <address@hidden>
| >
| >On 2-May-2000, Paul Kienzle <address@hidden> wrote:
| >
| >| How do I print to a file from an m-file without knowing the type of the
| >| thing I'm printing? disp(x) works fine to the terminal. I seem to want
| >| disp(x,fid).
| >
| >OK, in the current (2.1.x, CVS) sources, fdisp is now an M-file that
| >does this:
| >
| > function fdisp (fid, x)
| >
| > if (nargin == 2)
| > fid << x << "\n";
| > else
| > usage ("fdisp (fid, x)");
| > endif
| >
| > endfunction
| > ...
|
| Sorry to be so fickle, but I actually want it in a string.
OK, forget the M-file versions. I now have the following code in
pr-output.cc. Is this closer to what you want?
jwe
DEFUN (disp, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} disp (@var{x})\n\
Display the value of @var{x}. For example,\n\
\n\
@example\n\
disp (\"The value of pi is:\"), disp (pi)\n\
\n\
@print{} the value of pi is:\n\
@print{} 3.1416\n\
@end example\n\
\n\
@noindent\n\
Note that the output from @code{disp} always ends with a newline.\n\
\n\
If an output value is requested, @code{disp} prints nothing and\n\
returns the formatted output in a string.\n\
@end deftypefn\n\
@seealso{fdisp}")
{
octave_value retval;
int nargin = args.length ();
if (nargin == 1 && nargout < 2)
{
if (nargout == 0)
args(0).print (octave_stdout);
else
{
std::ostrstream buf;
args(0).print (buf);
buf << ends;
char *tmp = buf.str ();
retval = tmp;
delete [] tmp;
}
}
else
print_usage ("disp");
return retval;
}
DEFUN (fdisp, args, ,
"-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} fdisp (@var{fid}, @var{x})\n\
Display the value of @var{x} on the stream @var{fid}. For example,\n\
\n\
@example\n\
disp (stdout, \"The value of pi is:\"), disp (stdout, pi)\n\
\n\
@print{} the value of pi is:\n\
@print{} 3.1416\n\
@end example\n\
\n\
@noindent\n\
Note that the output from @code{disp} always ends with a newline.\n\
\n\
If an output value is requested, @code{disp} prints nothing and\n\
returns the formatted output in a string.\n\
@end deftypefn\n\
@seealso{disp}")
{
octave_value retval;
int nargin = args.length ();
if (nargin == 2)
{
int fid = octave_stream_list::get_file_number (args (0));
octave_stream os = octave_stream_list::lookup (fid, "fdisp");
if (! error_state)
{
ostream *osp = os.output_stream ();
if (osp)
args(1).print (*osp);
else
error ("fdisp: stream not open for writing");
}
}
else
print_usage ("fdisp");
return retval;
}
-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.che.wisc.edu/octave/octave.html
How to fund new projects: http://www.che.wisc.edu/octave/funding.html
Subscription information: http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------