help-octave
[Top][All Lists]
Advanced

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

Re: variable for filename in fopen


From: John W. Eaton
Subject: Re: variable for filename in fopen
Date: Mon, 18 Jan 2010 16:56:06 -0500

On 18-Jan-2010, Carlo de Falco wrote:

| On 18 Jan 2010, at 22:11, Blitz wrote:
| 
| > I need to read a bunch of txt files in a directory.  For some  
| > reason, fopen
| > does not like passing a variable instead of an explicitly declared  
| > string
| > (i.e. "File1.txt").  The snippet below creates an array with file  
| > names,
| > then is supposed to open those files, one by one.  If I replace  
| > 'reading'
| > with some string, it works.  As far as I can tell, the variable  
| > 'reading' is
| > a string, so this should work.  Ideas?
| >
| >
| >
| > list = ls *.txt;
| > for i = 1:length(list),
| >    reading = list(i, :)
| >    fid = fopen(reading, 'r') % open the file
| >    % do stuff
| > end;
| 
| have you actually tried to run this code in Octave? what Octave  
| version are you using?
| on what OS? for me (Octave 3.2.3 on OSX) the first line in your code  
| is invalid and results in a parse error.
| so I don't see how you can say that reading is a string...
| what happens if you run your code and then type just  
| 'reading' (without the quotes) at the octave prompt and press return?

In older versions of Octave you could write

  list = ls *.txt

but the result would be a matrix of characters, so each line was
padded with blanks.

If you want to keep using the old version of Octave and this method of
getting the list of files, then use

  reading = deblank (list(i,:));

But I would recommend that you upgrade to the current stable version
of Octave and use the dir function to get the list of files.  The dir
function will return the list of files as a cell array of character
strings instead of a character array, and so each element of the cell
array will not have any extra padding added.

Also, don't forget to close the files once you are done with them...

jwe


reply via email to

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