From: Judas Magnus on
I'm working with a text file that has a short story in it. And the txt name is called story.txt

In matlab i open the file by doing

shortStory = fopen('story.txt','r') % So that opens the story in read mode

Heres my question. I wish to make a for loop using fgetl to copy each line into a Cell Array...

I was thinking to also incorparate char and cellstr to help me accomplish this task. But I'm not sure how I would
From: Walter Roberson on
Judas Magnus wrote:
> I'm working with a text file that has a short story in it. And the txt
> name is called story.txt
>
> In matlab i open the file by doing
>
> shortStory = fopen('story.txt','r') % So that opens the story in read mode
>
> Heres my question. I wish to make a for loop using fgetl to copy each
> line into a Cell Array...
>
> I was thinking to also incorparate char and cellstr to help me
> accomplish this task. But I'm not sure how I would

Instead of using 'r' in your fopen(), you should be using 'rt' to
indicate you are working with a text file.

If you want to read each line into a cell array, the easiest way would be:

TheCellArray = textscan(shortStory, '%s', 'delimiter', '');

That would do the whole file in one command.

If you have your heart set on fgetl, then

TheCellArray = {};

while true
thisline = fgetl(shortStory);
if ~ischar(thisline); break; end
TheCellArray{end+1} = thisline;
end

No need for char() or cellstr() in this loop.
From: Judas Magnus on
yea textscan seems trivial for this, I was wondering how would I make this into a
for loop?

The way it is now, it seems it is a while loop while it is true.

for a for loop im not sure what would be the range.

Would it simply be
for k = 1:length(shortStory)
%then follow what you had with the while loop?
From: Walter Roberson on
Judas Magnus wrote:
> yea textscan seems trivial for this, I was wondering how would I make
> this into a for loop?
> The way it is now, it seems it is a while loop while it is true.
>
> for a for loop im not sure what would be the range.
>
> Would it simply be for k = 1:length(shortStory)
> %then follow what you had with the while loop?

shortStory, in this context, is the file identifier, which is a simple
double precision number, usually with a small integral value such as 3 .
The length() of a simple scalar like this would be 1 -- there is 1
number in the variable named shortStory .

You do not know ahead of time the number of lines you are going to
process, and there is no way to find out the number of lines that are in
a text file except to open the file and read through it line by line
counting the lines until you get to the end.

If you really really want to convert to a for loop, you could use

for K = 1 : inf

and then use the same inner logic as before. If this works at all, it
would not be any better than while true

If it makes you feel better to "do something" in the condition of the
loop, you could use

while ~feof(shortStory)

which would test the end of file indicator. However, a common mistake
that people make is in thinking that feof() knows whether there is any
more data in the file than has already been read, and thus thus people
believe that if feof is false that the next read will succeed. That is,
however, not how feof works: feof is not set until a read has been
attempted and failed because it encountered end of file. If you just
happened to have read right to the end of what is in the file, but have
not yet tried to read any more, then since no end-of-file was
encountered yet, feof will be false, but in such a case the read
following would fail. Thus, even if you use feof then you must still
test the output of the fgetl to see if an error condition occurred, and
break out of the loop... in which case you might as well skip the feof
test since it doesn't add anything useful.


If you were perhaps wondering: sorry, no, Matlab does not happen to have
a "repeat ... until" construct that is used in some languages for this
kind of situation.


Using "while true" is the recognized idiom amongst programmers for a
loop that will repeat an unknown number of times and where you cannot
tell at the start of the loop whether you will finish the loop this time
around or not.
From: Judas Magnus on
alrighty then! Thanks for the helpful and detailed response! I really appreciate it