alexanderd5398
alexanderd5398

Reputation: 327

How to format scientific notation with 1 digit after the decimal place in MATLAB?

I have tried sprintf("%.1e", x) but that gives me 6.3000e-16. How can I cut off the zeros and display just 6.3e-16? I am also displaying the numbers in a table.

EDIT: So now it is correctly display some numbers, but others aren't displayed in scientific notation at all. I am using R2018a.

Here is the code I am using

n       = zeros(19,1);
a       = zeros(19,1);
b       = zeros(19,1);
c       = zeros(19,1);
d       = zeros(19,1);

format short
for i = 2:20
    w = 1/(2000 * i);
    x = 1/(1000 * i);
    y = 1/(50 * i);
    z = 1/(20 * i);
    n(i-1)          = sprintf("%d", i);
    a(i-1)          = sprintf("%.1e", w);
    b(i-1)          = sprintf("%.1e", x);
    c(i-1)          = sprintf("%.1e", y);
    d(i-1)          = sprintf("%.1e", z);
end

table(  n, a, b, c, d )

and here is the output:

  19×5 table
    n        a          b         c         d   
    __    _______    _______    ______    ______
     2    0.00025     0.0005      0.01     0.025
     3    0.00017    0.00033    0.0067     0.017
     4    0.00013    0.00025     0.005     0.013
     5     0.0001     0.0002     0.004      0.01
     6    8.3e-05    0.00017    0.0033    0.0083
     7    7.1e-05    0.00014    0.0029    0.0071
     8    6.3e-05    0.00013    0.0025    0.0063
     9    5.6e-05    0.00011    0.0022    0.0056
    10      5e-05     0.0001     0.002     0.005
    11    4.5e-05    9.1e-05    0.0018    0.0045
    12    4.2e-05    8.3e-05    0.0017    0.0042
    13    3.8e-05    7.7e-05    0.0015    0.0038
    14    3.6e-05    7.1e-05    0.0014    0.0036
    15    3.3e-05    6.7e-05    0.0013    0.0033
    16    3.1e-05    6.3e-05    0.0013    0.0031
    17    2.9e-05    5.9e-05    0.0012    0.0029
    18    2.8e-05    5.6e-05    0.0011    0.0028
    19    2.6e-05    5.3e-05    0.0011    0.0026
    20    2.5e-05      5e-05     0.001    0.0025

Upvotes: 2

Views: 1439

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60444

In your code, you are assigning a string to a double-float array. It looks like MATLAB automatically converts the string to a double to store it there. Thus, your formatting gets lost:

>> sprintf("%.1e", 1/1000)
ans = 
    "1.0e-03"
>> a=0;
>> a(1) = sprintf("%.1e", 1/1000)
a =
   1.0000e-03
>> class(a)
ans =
    'double'

Instead, use a string array:

a = strings(19,1);
%...
   a(i-1) = sprintf("%.1e", w);

I'm not used to the new strings, and this behavior surprises me. Assigning a number to a string converts the number to a string, and assigning the string to a number converts it back to a number. This does not happen with the "old-fashioned" char arrays:

>> a=0;
>> a(1) = sprintf('%.1e', 1/1000);
Unable to perform assignment because the left and right sides have a different number of elements.

When using char arrays, store them in a cell array:

a = cell(19,1);
%...
   a{i-1} = sprintf('%.1e', w);

Upvotes: 1

Semborg O.
Semborg O.

Reputation: 1

You can use "fprintf":

>> fprintf("%.1f\n", pi)
3.1

To show more or less digits just adjust the number after the dot.

Upvotes: 0

Related Questions