John Stone
John Stone

Reputation: 715

Matlab num2str (A, format) function component-wise?

I have

    a=[0.221354766 315.806415];

I want sth like (same fieldwidth)

    0.2214     315.8064

I tried

    b=num2str(a) 
    % b =
    % 0.2213548      315.8064
    c=num2str(a,'%8.4f')
    % c =
    % 0.2214315.8064
    d=num2str(a,'%8.7g')
    %d =
    %0.2213548 315.8064

Any suggestion? Tks

Upvotes: 0

Views: 319

Answers (2)

Wolfie
Wolfie

Reputation: 30047

If you want to store these values as strings, then by all means use num2str(a, '%.4f'). It seems odd to take a numerical martrix and store all of the values as strings though, to just round the result use round

m = round([0.221354766 315.806415], 4)

>> m = [0.2214, 315.8064]

Upvotes: 0

MrAzzaman
MrAzzaman

Reputation: 4768

If I understand you correctly, you want the same number of decimal places? If this is the case, just leave off the first number in your format string:

num2str([0.221354766 315.806415],'%.4f ')

ans =

    '0.2214 315.8064'

Upvotes: 2

Related Questions