help using fgetl???
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
In the problem that i am currently on i have to use the data file mm.dat that looks like this
{33,2,11}
{45,9,3}
and use fgetl in a loop to read the data in. Then i am to create a matrix that stores just the numbers, and write the matrix to a new file. Assume that each line in the original file contains the same number of numbers.
i have this so far, but i cant figure out how to read only the numbers from the data and not the commas or the {}.
cat = fopen('mm.dat');
if cat == -1
disp('file did not open')
else
while feof(cat) == 0
evrline = fgetl(cat)
num = strtok(evrline)
save probtwenonewfile num
end
closeresult = fclose(cat);
end
0 commentaires
Réponse acceptée
Walter Roberson
le 27 Mar 2012
Please read my answer and associated comments in
7 commentaires
Walter Roberson
le 27 Mar 2012
Quoting myself:
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.
=====
Now are you testing the result of the fgetl(cat) before you use it? No you are not: you are relying on feof(cat) to *predict* end of file. Which is not what feof() does.
The great majority of loops written as while ~feof() are wrong in practice. Correct structure usually calls for
while true
attempt the read
check to see if the read generated end of file
if end of file was generated, break out of the loop
otherwise process the line
end
Plus de réponses (1)
Geoff
le 27 Mar 2012
Is this an assignment for a course you are doing?
Do you have to use strtok? Or can you use something like regexp or textscan?
Using strtok is okay, if you use it properly:
doc strtok
You will need to extract one token at a time until there are no more. Hint: set delimiters to any characters you don't want.
Are the numbers non-negative integers?
I would probably use regexp.
4 commentaires
Geoff
le 27 Mar 2012
You really need to read the documentation for these functions. Read, then experiment, and you will become familiar. Type in 'help regexp' for quick help, or 'doc regexp' for more detailed help.
Voir également
Catégories
En savoir plus sur Text Data Preparation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!