Why are the values I write into a file are not the same as what I read from it?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Prashanth
am 13 Dez. 2013
Beantwortet: Walter Roberson
am 13 Dez. 2013
I use dlmwrite to write a matrix into a .txt file. If I then use dlmread to take these values from the file and compare this matrix with the original matrix, the values are not exactly the same, i.e., the accuracy of the floating digits is lost. Is there a way to exactly store the matrix values? Thanks in advance.
0 Kommentare
Akzeptierte Antwort
David Sanchez
am 13 Dez. 2013
Use fprintf and fscanf :
x = 0:.1:1;
A = [x; exp(x)];
% write to file
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
% read the file
fid = fopen('exp.txt');
A = fscanf(fid, '%g %g', [2 inf]);
fclose(fid);
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!