Re: reading file list, writing to text file



d=dir('*.jpg');
for k=1:length(d)
fname=d(k).name;
fname %check it is reading all files
fid=fopen('list.txt', 'w');
count = fprintf(fid, fname);

fclose(fid)
end

The problem I have is that even though the program correctly reads
through all the files, only the last read file is stored in the text
file....I presume the text file is just constantly being overwritten.

Hello Monkey,
The reason your loop is overwriting the previous text file is that you
are opening the text file not in append mode.

Also, you should not open the file for reading multiple times - that is
the slowest part of this loop by far. You ought to store all the file
names in an array and at the end of the loop, write the entire array to
file, just once.

d=dir('*.jpg');
myfname = [];
for k=1:length(d)
myfname = [myfname d(k).name ' ' ];
end

fid = fopen('list.txt', 'w');
count = fprintf( fid, myfname );

Does this help, I hope?

~Helen Lurie

.



Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: advice and/or help on glReadPixels
    ... I could not see any "BRGA" in the documentation. ... Is it possible that some cards are not ... >>> a) Try reading the entire RGB instead of just the red. ... a quick integer loop with a mask & shift is ...
    (comp.graphics.api.opengl)
  • RE: 2.6.28-rc6-mmotm1126 - acpi AE_AM_INFINITE_LOOP errors..
    ... You could try making the max loop count larger, ... This is a Dell Latitude D820, and didn't seem to have any infinite AML ... reading AC Adapter state ...
    (Linux-Kernel)
  • Re: while (1) vs. for ( ;; )
    ... >to write to the next guy who will be reading the code. ... You are unfamiliar with the term "infinite loop"? ... Balmer Consulting ...
    (comp.lang.c)

Loading