help using fgetl function???
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposte (3)
Geoff
il 17 Apr 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 =)
0 Commenti
Vedere anche
Categorie
Scopri di più su Text Data Preparation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!