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!