Display a variable and its units
이전 댓글 표시
I know how to use the display(X) function to display a variable I have solved for. So, if my X = 25, and I need it to display 25 ft/lb, how would I do this?
댓글 수: 1
Dianne Claire Nocon
2021년 5월 7일
fprintf('The answer is %.2f ft/lb.\n', x)
채택된 답변
추가 답변 (3개)
Thomas Green
2018년 2월 22일
편집: Thomas Green
2018년 2월 22일
16 개 추천
Hey, I just want to thank you guys for 1. always making it more complicated than it needs to be 2. adding statements/functions that are never really seen but could have easily could have produced the same result with a common command and most importantly 3.NEVER SHOWING AN OUTPUT. Like do you really think you are helping????
Sven
2011년 12월 3일
You can use fprintf as follows:
X = 25;
fprintf('The answer is %d ft/lb\n', X)
If your answer will not be a nice round number like "25", you can replace the %d above with, say, %0.2f to print two decimal places.
댓글 수: 5
Ryan
2011년 12월 3일
Karan Gill
2017년 4월 7일
Units are now in MATLAB if you have Symbolic Math Toolbox. See the tutorial: https://www.mathworks.com/help/symbolic/units-of-measurement-tutorial.html
Posted a separate answer below so that OP can accept.
Damien Fernando
2019년 9월 28일
Great answer, ty
Samuel Katongole
2020년 3월 19일
Wow..this by Sven works...
Rakesh Chaudhary
2021년 1월 23일
good..thanks
Ryan
2011년 12월 3일
0 개 추천
댓글 수: 1
Michael Darwish
2021년 12월 12일
Thank you, that is exactly what I was looking for.
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!