help using fgetl function???

5 次查看(过去 30 天)
Joey
Joey 2012-4-17
Have to do a problem and it is written as such;
A Software package write data to a file in a format that includes curly braces around each line and commas separating the values. for example, a data file mm.dat might look like this:
{33,2,11}
{45,9,3}
Use the fgetl function in a loop to read these data in. 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.
now i have the following code
i have the numbers in the braces in a .dat file just as they appeared previously
and my loop is such
fid = fopen('mm.dat','w');
while feof(fid) ~ 0;
aline = fgetl(fid)
fprintf(fid,'%f %f %f \n',aline)
end
closeresult = fclose(fid);
save aline
but when i proceed with the code it just keeps running for ever. obviously something is wrong here but i cant really figure it out.

回答(3 个)

Honglei Chen
Honglei Chen 2012-4-17
you should use
while ~feof(fid)
  12 个评论
Honglei Chen
Honglei Chen 2012-4-17
But you said you need to write it to a new file, right?
Joey
Joey 2012-4-17
hmm correct i guess i dont really understand what it means to write it to a new file

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2012-4-17
  1 个评论
Joey
Joey 2012-4-17
but this is a data file not a txt file and im reading numbers and characters. hmmmmm soo should i use ~ishchar somehwere

请先登录,再进行评论。


Geoff
Geoff 2012-4-17
Mate, you're doing something odd.
For starters, you're opening the file in 'write' mode but you are clearly wanting to read from it. That call is going to erase the contents of the file.
So do this (note the 'r')
fid = fopen('mm.dat','r');
And as Honglei Chen suggested:
while ~feof(fid)
Now, if you want to read the numbers out of the line you have just read, you would use sscanf, not fprintf. A call to fprintf tries to WRITE to the file (ignoring that the parameters are wrong here). You shouldn't do that while you are reading from it:
ldata = sscanf( aline, '%f', 3 );
But this won't work because you have curly braces and commas to deal with. You need to use textscan (or regexp):
doc textscan
Since this looks to be a homework assignment, I'll leave you to work out how, by reading through the link that Walter gave you.
Almost nobody in my profession bothers to get the result of an fclose, so just this is fine:
fclose(fid);
Finally, you are not creating a matrix. You are saving aline at the end which will contain the text of the last line you have read. Not a matrix of numbers you have extracted. You need something like this:
mydata = [];
while ~feof(fid)
aline = fgetl(fid);
ldata = sscanf( aline, '%f', 3 ); % Remember, this line will NOT work.
mydata(end+1,:) = ldata;
end
That will append each row of numbers to your matrix mydata.
As a footnote, I'd like to point out that Walter's answer is entirely relevant to your question and you need to READ that page. You are reading a text file. It doesn't matter that it is called 'whatever.dat'. It is comprised of only text as opposed to binary data. In fact, you should probably open it in 'rt' mode (not just 'r') with fopen as general practise... especially so if you are using Windows. So, bottom line is that I wouldn't argue with Walter =)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by