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

1 次查看(过去 30 天)
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.

采纳的回答

David Sanchez
David Sanchez 2013-12-13
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);

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-12-13
Add the dlmwrite option 'precision', '%.17g'

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by