help using fgetl???

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

 採用された回答

Walter Roberson
Walter Roberson 2012 年 3 月 27 日

0 投票

7 件のコメント

Joey
Joey 2012 年 3 月 27 日
i dont see how that helps me out really
Walter Roberson
Walter Roberson 2012 年 3 月 27 日
You are misusing feof() and my explanation in that posting tells you exactly what the problem with feof() is.
Joey
Joey 2012 年 3 月 27 日
to my knowledge i believe that interpreted the feof(cat) means that while were not at the end of cat do this. This being fgetl and so forth.
Joey
Joey 2012 年 3 月 27 日
i see what you are trying to do though. Your reading each line with fgetl then using the ischar function to see if the line contains a character
Joey
Joey 2012 年 3 月 27 日
could i use the num2str function before my fgetl to create all the numbers to strings and would that make it any better
Walter Roberson
Walter Roberson 2012 年 3 月 27 日
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
Joey
Joey 2012 年 3 月 27 日
okay i'm just really confused now jeeze,

サインインしてコメントする。

その他の回答 (1 件)

Geoff
Geoff 2012 年 3 月 27 日

0 投票

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 件のコメント

Joey
Joey 2012 年 3 月 27 日
Okay well yes this is a problem for a homework and i do not have to use strtok it was just my attempt in trying to figure out how to separate the commas and the {} from the numbers but i couldn't really figure out that ether.
Joey
Joey 2012 年 3 月 27 日
and i am unfamiliar with the regexp function
Geoff
Geoff 2012 年 3 月 27 日
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.
Joey
Joey 2012 年 3 月 27 日
i did ,,,but see if used that function i probably would recieve no credit for the problem as we have never used it in class

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by