MATLAB Answers

0

Read an input file- process it line by line

Asked by PiParadox on 26 Nov 2011

Hi I'm trying to figure out how to process an input file, where you check to see if each line is a word. But I don't have any clue on how to begin doing so. I would use something like "load" I think? Then I would save that word in a variable.

  0 Comments

Log in to comment.

1 Answer

Answer by Walter Roberson
on 26 Nov 2011
 Accepted Answer

%at this point, insert the code to initialize the variable you will be storing the words in
%then
fid = fopen('YourFile.txt','rt');
while true
  thisline = fgetl(fid);
  if ~ischar(thisline); break; end  %end of file
    %now check whether the string in thisline is a "word", and store it if it is.
    %then
  end
  fclose(fid);
%you have now loaded all of the data.

  3 Comments

Thank you for your help. I'm a little confused why the "while true" part is there? Is true satisfied by "fopen" being valid?
Also I'm going to be putting this into a function.
It seems that the while loop ends when the line processer (fget1) encounters a non-word with the break. I'm hoping that this function continues to search through the rest of the file, checking if they are valid words, then entering them into a cell. Will this occur? (Can you also elaborate on how to make a cell that can be used throughout a function, not just within this subfunction? Global cell I think?)
Sorry for the long question!!

why not to use while ~feof(fid)?!

"while true" is an infinite loop. The loop will be exited by the "break" statement in the "if" inside the loop.

fgetl() returns a string ("string" is defined as an array of type char) *except* when fgetl() encounters end of file. The test is thus to see whether end of file has been it. It is not a test for a "non-word". You have not defined what a "word" or "non-word" is, so you will have to put that in at the point I show the comment.

feof(fid) only ever becomes true when there is no input remaining _and_ a read operation then attempts to read input that is not there. feof() does not look forward to see whether there is more input or not: it just reports on whether the eof flag has been set already by a read operation on an empty buffer having tried and failed to get input. If you had the case where, for example, you had a file that ended in XYZPDQ with no end-of-line indicator, then when a read operation got to that line, it would read the data that is there but *not* set the end-of-line indicator (in this case the buffer was not empty), and feof would not report true until the *next* read attempt found the buffer empty and nothing more to fetch.

This is how the C language standards and POSIX *define* end of file processing.

Because of this, you must test the result of each read operation, to see whether that was the read operation that found end-of-file.

If feof(fid) is true then reading from fid would fail, but feof() is not predictive, so the fact that feof() is false does not mean that a read will succeed.

Log in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today