help using fgetl function???

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
Honglei Chen el 17 de Abr. de 2012

0 votos

you should use
while ~feof(fid)

12 comentarios

Joey
Joey el 17 de Abr. de 2012
so ~feof(fid) still didnt work it just keeps say ans = 0 and aline = 0 forever
Honglei Chen
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
Joey el 17 de Abr. de 2012
hmmm okay well i need to read and w to it so should i read it then use a w command later in the code
Joey
Joey el 17 de Abr. de 2012
fid = fopen('mm.dat','w');
while ~feof(fid);
aline = fgetl(fid);
[num] = aline;
end
fin = fopen('num','w');
fprintf(fid,'%f %f %f \n',num)
closeresult = fclose(fid);
save num
tried this but still doesnt work
Honglei Chen
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
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
Joey el 17 de Abr. de 2012
hmm i dont understand why you use 'num.dat' its not a data file its just a matrix that i extracted from the mm.dat file
Joey
Joey el 17 de Abr. de 2012
In ==> problemtwenty at 6
aline = fgetl(fid);
keep getting that
Honglei Chen
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
Joey el 17 de Abr. de 2012
but there is only one data file i have to open that one data file read only the numbers out into a matrix and modifiy that matrix
Honglei Chen
Honglei Chen el 17 de Abr. de 2012
But you said you need to write it to a new file, right?
Joey
Joey el 17 de Abr. de 2012
hmm correct i guess i dont really understand what it means to write it to a new file

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 17 de Abr. de 2012

0 votos

1 comentario

Joey
Joey el 17 de Abr. de 2012
but this is a data file not a txt file and im reading numbers and characters. hmmmmm soo should i use ~ishchar somehwere

Iniciar sesión para comentar.

Geoff
Geoff el 17 de Abr. de 2012

0 votos

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

Preguntada:

el 17 de Abr. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by