While loops

  • Follow


In C the canonical way of reading data from a file is this

while ( (ch = fgetc(fp) ) != EOF)
   process(ch);

In Matlab the synatx

while( (line = fgets(fp)) ~= -1)

is not accepted. Whilst of course it's possible to build a loop with several fgets() and tests, there doesn't seem to be a non-clumsy way of achieving iteration over an input file.

 
0
Reply Malcolm 6/1/2010 2:12:04 PM

"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message <hu34fk$ggq$1@fred.mathworks.com>...
> In C the canonical way of reading data from a file is this
> 
> while ( (ch = fgetc(fp) ) != EOF)
>    process(ch);
> 
> In Matlab the synatx
> 
> while( (line = fgets(fp)) ~= -1)
> 
> is not accepted. Whilst of course it's possible to build a loop with several fgets() and tests, there doesn't seem to be a non-clumsy way of achieving iteration over an input file.
> 
>  

Can you define "non-clumsy"?
0
Reply someone 6/1/2010 2:23:06 PM


"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message <hu34fk$ggq$1@fred.mathworks.com>...
> In C the canonical way of reading data from a file is this
> 
> while ( (ch = fgetc(fp) ) != EOF)
>    process(ch);
> 
> In Matlab the synatx
> 
> while( (line = fgets(fp)) ~= -1)
> 
> is not accepted. Whilst of course it's possible to build a loop with several fgets() and tests, there doesn't seem to be a non-clumsy way of achieving iteration over an input file.
> 

You cannot do an assignment in the while statement
itself in matlab. However, nothing stops you from
doing it as:

ch = fgetc(fp);
while ( ch ~= EOF)
   process(ch);
   ch = fgetc(fp);
end
 
The above loop hardly seems clumsy. WTP?

John
0
Reply John 6/1/2010 2:31:06 PM

Malcolm McLean wrote:
> In C the canonical way of reading data from a file is this
> 
> while ( (ch = fgetc(fp) ) != EOF)
>   process(ch);
> 
> In Matlab the synatx
> 
> while( (line = fgets(fp)) ~= -1)
> 
> is not accepted. Whilst of course it's possible to build a loop with 
> several fgets() and tests, there doesn't seem to be a non-clumsy way of 
> achieving iteration over an input file.

Correct, assignment is not an expression in Matlab.

There is probably a one-line way to do it involving subsref, eval, and 
anonymous functions, but I'd classify that as clumsy.

You do not need several tests, though:

while true
   line = fgets(fp);
   if line == -1; break; end
   % body here
end


(Say... don't I remember you from comp.lang.c ?)
0
Reply Walter 6/1/2010 2:35:33 PM

"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
news:hu34fk$ggq$1@fred.mathworks.com...
> In C the canonical way of reading data from a file is this
>
> while ( (ch = fgetc(fp) ) != EOF)
>   process(ch);
>
> In Matlab the synatx
>
> while( (line = fgets(fp)) ~= -1)
>
> is not accepted. Whilst of course it's possible to build a loop with 
> several fgets() and tests, there doesn't seem to be a non-clumsy way of 
> achieving iteration over an input file.

while ~feof(fp) % While we haven't reached EOF
    ch = fgetl(fp); % read the next line
    process(ch); % and process the read line
end

It's fairly compact and I would call this non-clumsy.  In fact, if you don't 
need the retrieved line anywhere else, you could combine the body of the 
loop into one command.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 


0
Reply Steven 6/1/2010 3:16:31 PM

Steven Lord wrote:

> while ~feof(fp) % While we haven't reached EOF
>     ch = fgetl(fp); % read the next line
>     process(ch); % and process the read line
> end

As in C, that will not work. feof() does not predict EOF, it tests to 
see if EOF has been encountered already. One must test ch after the 
fgets() or fgetl().

feof() cannot predict EOF because more data might be added to the file 
or socket or serial port while the program is waiting (or busy), so 
feof() is restricted to telling you that you attempted to do a read and 
no data was found.
0
Reply Walter 6/1/2010 3:28:16 PM

5 Replies
363 Views

(page loaded in 0.045 seconds)

Similiar Articles:













7/22/2012 11:45:55 AM


Reply: