필터 지우기
필터 지우기

Choosing decimal places by user input.

조회 수: 3 (최근 30일)
Stephanie Velasco
Stephanie Velasco 2016년 11월 12일
댓글: the cyclist 2016년 11월 13일
I was trying to write a code where I can ask the user to choose the number of decimal he/she wants to use when calculating the min or max of vector. But I am really stuck
I was trying to do what below but it doesn't work.
decimal = input('Type the number of decimals places to show in output:');
vector = [23 28 4.29 40.2 8];
min_ = min(Vector);
fprintf('\nMininum: %0.(%0.f)f',min_,decimal);

답변 (2개)

Walter Roberson
Walter Roberson 2016년 11월 13일
decimal = input('Type the number of decimals places to show in output:');
vector = [23 28 4.29 40.2 8];
min_ = min(Vector);
fprintf('\nMininum: %0.*f', decimal, min_);
The * in the format signals that the number of decimal places should be extracted from the parameters.
  댓글 수: 1
the cyclist
the cyclist 2016년 11월 13일
Nice. Was unaware of that functionality (and missed it when I scanned the documentation).

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


the cyclist
the cyclist 2016년 11월 12일
I don't know if you can get the formatSpec itself as an input, so instead you can embed the formatSpec as a separate call to sprintf:
decimal = input('Type the number of decimals places to show in output:');
vector = [23 28 4.29 40.2 8];
min_ = min(vector);
fprintf(['\nMininum: %0.',sprintf('%d',decimal),'f\n'],min_);
  댓글 수: 1
Stephanie Velasco
Stephanie Velasco 2016년 11월 12일
It worked perfectly! thank you so much!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by