[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help octave

From: Ted Harding
Subject: Re: help octave
Date: Tue, 15 Aug 1995 09:32:59 +0200 (BST)

( Re Message From: Shu-Kit Yeung )
> Could someone please tell me how to use "fprintf" to print a matrix into 
> a file? Thank You!

The following, "write.m", (adapted from a very old matlab file of mine)
does that.  It is slow in octave. Others may have a better solution.

Best wishes,
Ted.                                    (address@hidden)
function write(file,fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
#H      write('filename','format',x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
#H      writes the data in the rows of matrices x1,x2,...
#H      side-by-side into the rows of <filename> (APPEND mode).
#H      'format' is a C f,e or g format (e.g.'%7.4f') for a single number.
#H      'format' is mandatory, 'filename' is optional and, if omitted,
#H      output is to the screen. If 'filename' exists the output is appended.
#H      There must be at least one matrix argument x1, and at most 10.
#H      The matrices x1, x2, ... are output side by side; all must have the
#H      same number of rows.
s_temp = empty_list_elements_ok;
empty_list_elements_ok = "true";
if isstr(fmt), strargs=2; end
if strargs==1, FIL="stdout"; X=fmt; FMT=file; P=1;
  else X=[]; FMT=fmt; FIL=file; P=0; end

if (nargin-strargs)>P+0, X=[X x1]; end
if (nargin-strargs)>P+1, X=[X x2]; end
if (nargin-strargs)>P+2, X=[X x3]; end
if (nargin-strargs)>P+3, X=[X x4]; end
if (nargin-strargs)>P+4, X=[X x5]; end
if (nargin-strargs)>P+5, X=[X x6]; end
if (nargin-strargs)>P+6, X=[X x7]; end
if (nargin-strargs)>P+7, X=[X x8]; end
if (nargin-strargs)>P+8, X=[X x9]; end
if (nargin-strargs)>P+9, X=[X x10]; end
S=size(X); R=S(1); C=S(2);
for i=1:R
  for j=1:C
    fprintf(FIL,FMT,X(i,j)), fprintf(FIL,' '), end, fprintf(FIL,'\n')
if (strargs==2) fclose(FIL); end
empty_list_elements_ok = s_temp;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]