limit the -ascii output to two decimal points

9 Ansichten (letzte 30 Tage)
sagarika rath
sagarika rath am 26 Feb. 2023
Kommentiert: Walter Roberson am 26 Feb. 2023
save(fullfile(PathName,filename),'x','-ascii');
how to save the files upto two decimals

Antworten (1)

Walter Roberson
Walter Roberson am 26 Feb. 2023
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 Kommentare
sagarika rath
sagarika rath am 26 Feb. 2023
i tried to save the outputs two digits after the decimal using dlmwrite option and its not working.
Walter Roberson
Walter Roberson am 26 Feb. 2023
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by