help using fgetl function???
Mostrar comentarios más antiguos
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.
Respuestas (3)
Honglei Chen
el 17 de Abr. de 2012
you should use
while ~feof(fid)
12 comentarios
Joey
el 17 de Abr. de 2012
Honglei Chen
el 17 de Abr. de 2012
Just noticed that you opened the file for writing. Are you trying to read or write? fgetl is for reading the file, not writing the file.
Joey
el 17 de Abr. de 2012
Joey
el 17 de Abr. de 2012
Honglei Chen
el 17 de Abr. de 2012
I thought you need to write to a new file. So you should use fopen('mm.dat','r') and fgetl to read from mm.dat. You can then use fopen('foo.dat','w') and fprintf to write the result to foo.dat, a new file. Even easier, you can also consider using save command to save the matrix.
Honglei Chen
el 17 de Abr. de 2012
Your code has several issues, it mixes fin and fid. Als thre is no need to do save at the end. I rewrote it below. I'm not sure if it does what you do though.
fid = fopen('mm.dat','r');
fin = fopen('num.dat','w');
while ~feof(fid);
aline = fgetl(fid);
fprintf(fid,'%f %f %f \n',aline);
end
fclose(fid);
fclose(fin);
Also, I thought you need to take care of those bracket too. But nevertheless, I think above is what you need to do as a framework
Joey
el 17 de Abr. de 2012
Joey
el 17 de Abr. de 2012
Honglei Chen
el 17 de Abr. de 2012
I'm just using that as an example of file name, you could call it bar.dat or anything you want.
Joey
el 17 de Abr. de 2012
Honglei Chen
el 17 de Abr. de 2012
But you said you need to write it to a new file, right?
Joey
el 17 de Abr. de 2012
Walter Roberson
el 17 de Abr. de 2012
0 votos
1 comentario
Joey
el 17 de Abr. de 2012
Geoff
el 17 de Abr. de 2012
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 =)
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!