is it possible to print 5decimal using disp commant

1 回表示 (過去 30 日間)
mehmet salihi
mehmet salihi 2021 年 5 月 18 日
コメント済み: Rik 2021 年 5 月 31 日
disp([x' u])
assume x and u are matrix
when i excute this, i get 0.9859.
i want to show with 5 decimal after point. 0.96875
  2 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 5 月 18 日
hello
you can use sprintf
% Examples
% sprintf('%0.5g',(1+sqrt(5))/2) % 1.618
% sprintf('%0.5g',1/eps) % 4.5036e+15
% sprintf('%15.5f',1/eps) % 4503599627370496.00000
% sprintf('%d',round(pi)) % 3
Rik
Rik 2021 年 5 月 18 日
@Mathieu NOE feel free to move your comment to the answer section, as I would say this is the answer.
@mehmet salihi you can either look at sprintf and fprintf or control the display of values in general with the format function.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2021 年 5 月 19 日
I'd use fprintf(), not sprintf(). fprintf() prints directly to the command window while sprintf() sends the result to a string. If you didn't specify a string, it will add "ans = " to the result, or the string variable name if you specified one, and enclose the number in single quote marks.
Compare these 3 different ways of doing it:
val = 0.96875123456789
fprintf('%.5f\n', val); % Method 1 using fprintf
sprintf('%.5f', val) % Method 2 using sprintf without assigning it to anything.
str = sprintf('%.5f', val) % method 3 using sprintf and assigning it to a variable called "str".
Results in command window:
0.96875
ans =
'0.96875'
str =
'0.96875'
  2 件のコメント
mehmet salihi
mehmet salihi 2021 年 5 月 31 日
i have matrix, not only one variable, how can i apply it to my matrix elements.
and i use the command below.
y=2:-0.05:0;
Told is matrix
disp([y' Told(:,find(abs(x-0.0) < 0.001)) Told(:,find(abs(x-0.2) < 0.001))...
Told(:,find(abs(x-0.4) < 0.001)) Told(:,find(abs(x-0.6) < 0.001))...
Told(:,find(abs(x-0.8) < 0.001)) Told(:,find(abs(x-1.0) < 0.001))])
Rik
Rik 2021 年 5 月 31 日
Have you read the documentation for sprintf or fprintf?
Note that Matlab stores (and reads) arrays in column-major form, so you might need to transpose your matrix to get the expected result:
A=reshape(1:4,2,2);
disp(A)
1 3 2 4
fprintf('%d %d\n',A)
1 2 3 4
fprintf('%d %d\n',A.')
1 3 2 4

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by