Display a variable and its units

조회 수: 215 (최근 30일)
Ryan
Ryan 2011년 12월 3일
댓글: Michael Darwish 2021년 12월 12일
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
Dianne Claire Nocon 2021년 5월 7일
fprintf('The answer is %.2f ft/lb.\n', x)

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

채택된 답변

Karan Gill
Karan Gill 2017년 4월 7일
편집: Karan Gill 2017년 10월 17일
Units are now in MATLAB if you have Symbolic Math Toolbox. Try this, and see the tutorial: https://www.mathworks.com/help/symbolic/units-of-measurement-tutorial.html:
>> u = symunit;
>> x = 2*u.meter
x =
2*[m]
>> x = rewrite(x,u.ft)
x =
(2500/381)*[ft]
>> double(separateUnits(x))
ans =
6.5617
  댓글 수: 3
Samuel Katongole
Samuel Katongole 2020년 3월 19일
I am also looking for somwthing similar to what Damien is looking for, but mine is when i want to print thw units along with the values especially using the fprintf function....I too need help here..
Husam Alsaleh
Husam Alsaleh 2020년 9월 24일
i am also using fprintf and i need to display my units next to the numbers

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

추가 답변 (3개)

Thomas Green
Thomas Green 2018년 2월 22일
편집: Thomas Green 2018년 2월 22일
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????
  댓글 수: 2
Thomas Jeffrey
Thomas Jeffrey 2018년 11월 5일
True that
Medinah Lee
Medinah Lee 2019년 5월 13일
LOL Preach!

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


Sven
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
Samuel Katongole
Samuel Katongole 2020년 3월 19일
Wow..this by Sven works...
Rakesh Chaudhary
Rakesh Chaudhary 2021년 1월 23일
good..thanks

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


Ryan
Ryan 2011년 12월 3일
I got it figured out. Thanks for the help! If I do X = 25; fprintf('The answer is %.2f ', X) display('unit');
It will put The answer is 25.00 unit and then output my next answer on the next line.
  댓글 수: 1
Michael Darwish
Michael Darwish 2021년 12월 12일
Thank you, that is exactly what I was looking for.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by