Filter löschen
Filter löschen

Why are the values I write into a file are not the same as what I read from it?

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

David Sanchez
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)

Walter Roberson
Walter Roberson am 13 Dez. 2013
Add the dlmwrite option 'precision', '%.17g'

Kategorien

Mehr zu Low-Level File I/O 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!

Translated by