How to round a result to two decimal places, Part 2

조회 수: 2 (최근 30일)
alpedhuez
alpedhuez 2020년 12월 15일
댓글: Jan 2020년 12월 16일
shows how to round the result to two decimal places.
Here what I would want Matlab to do is to round the display to two demical places.
(1) I have
Fit = polyfit(series_1,series_2, 1);
theString = sprintf('(dependent variable) = %.3f*(independent variable) %.3f', Fit(1), Fit(2));
How can one let Matlab display only up to two demical places?
(2) I have
corr=corrcoef(series_1,series_2);
num2str(corr(1,2),3)
How can one let Matlab display only up to two demical places?
(3) I have
tbl=anova(mdl);
tbl2=table2cell(tbl('x1',4)) % Get F-statistics
tbl3=string(tbl2);
How can one let Matlab display only up to two demical places?
  댓글 수: 3
alpedhuez
alpedhuez 2020년 12월 16일
I meant in the output.
Jan
Jan 2020년 12월 16일
What is "the output"? You have used sprintf('%.3f') to create 3 decimal places, so thge reader can assume, that you can simply use the '%.2f' command. See: doc sprintf
In the documentation of num2str you find, that you cn use the same format specifier as in sprintf. See Walter's answer.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 16일
series_1 = 1:10;
series_2 = cos(rand(1,10));
Fit = polyfit(series_1,series_2, 1);
theString = sprintf('(dependent variable) = %.2f*(independent variable) %.2f', Fit(1), Fit(2));
% ^ ^
disp(theString)
(dependent variable) = 0.02*(independent variable) 0.70
corr=corrcoef(series_1,series_2);
num2str(corr(1,2),2)
ans = '0.6'
% ^
tbl2 = {rand}
tbl2 = 1x1 cell array
{[0.3002]}
string(tbl2)
ans = "0.30025"
compose("%.2f", tbl2{:})
ans = "0.30"
sprintf('%.2f', tbl2{:})
ans = '0.30'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by