limit the -ascii output to two decimal points

5 次查看(过去 30 天)
save(fullfile(PathName,filename),'x','-ascii');
how to save the files upto two decimals

回答(1 个)

Walter Roberson
Walter Roberson 2023-2-26
You cannot do that -- the -ascii flag always outputs a number of digits . However you can
format long g
PathName = '.'; filename = 'test.txt';
x = rand(1,11).' .* 10.^(-5:5).'
x = 11×1
1.0e+00 * 2.49705612941576e-06 3.21530394035424e-05 0.000988743454538754 0.00709450963686247 0.00580482761786273 0.627987242677571 1.0061090249889 11.1394065600517 242.27180145787 3358.60943186604
%if you mean two digits after the decimal place
dlmwrite(fullfile(PathName, filename), x, 'precision', '%.2f')
dbtype(fullfile(PathName, filename))
1 0.00 2 0.00 3 0.00 4 0.01 5 0.01 6 0.63 7 1.01 8 11.14 9 242.27 10 3358.61 11 64152.44
%if you mean two significant digits
temp = round(x, 2, 'significant');
dlmwrite(fullfile(PathName, filename), temp)
dbtype(fullfile(PathName, filename))
1 2.5e-06 2 3.2e-05 3 0.00099 4 0.0071 5 0.0058 6 0.63 7 1 8 11 9 240 10 3400 11 64000
  3 个评论
sagarika rath
sagarika rath 2023-2-26
i tried to save the outputs two digits after the decimal using dlmwrite option and its not working.
Walter Roberson
Walter Roberson 2023-2-26
The above example shows that using dlmwrite with 'precision' does work.. well, except possibly not for infinite or nan values, or for non-numeric values.
What output are you observing when you try?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by