help-octave
[Top][All Lists]
Advanced

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

looping over file objects


From: John W. Eaton
Subject: looping over file objects
Date: Thu, 13 Nov 2003 21:51:41 -0600

On 13-Nov-2003, Martijn Brouwer <address@hidden> wrote:

| I would like to print data to both stdout and a file. The way I thought to do 
this was:
| 1) file=fopen()
| 2) files=[stdout,file]
| and subsequently:
| for f=files
|     fprintf(f,....)
| end
| 
| But it seems that file objects cannot be inserted in an array. This was no 
problem when file objects were just integers. How can I do what I want in an 
elegant way?
| I do it now in this way, but it is an ugly hack:
| files_to_process=1; %for stdout
| file=fopen(...);
| files_open=fopen("all");
| filenum=files_open(length(file_open));
| files_to_process=[files_to_process,filenum];
| 
| for f=files_to_process
| ...
| end

As workarounds, here are two ways, both of which should be compatible
with Matlab.

First, adding 0 to a file object will convert it to a number, so you
can write

  files = [stdout+0, file+0]

or, you could store them in a cell array instead:

  files = {stdout, file}
  for i = 1:length (files)
    fprintf (files{i}, ...);
  end

I've added a few more functions to the CVS version of src/ov-file.h so
that file objects will act like scalar values in more places, so your
original idea will work, but my changes may not provide complete
compatibility.  So, this brings up a question about what Octave should
really do for file ids.  I like the idea of having more information
about a file stored in the object returned from fopen, but since it
causes compatibility problems, perhaps fopen should just return a
scalar value.  Comments?

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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