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
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 日

0 投票

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 件のコメント

Damien Fernando
Damien Fernando 2019 年 9 月 28 日
Not useful at all
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 日

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????

2 件のコメント

Thomas Jeffrey
Thomas Jeffrey 2018 年 11 月 5 日
True that
Medinah Lee
Medinah Lee 2019 年 5 月 13 日
LOL Preach!

サインインしてコメントする。

Sven
Sven 2011 年 12 月 3 日

5 投票

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
Ryan 2011 年 12 月 3 日
Thank you! My variable is 7460.1 lbf That works very well, however it printed "The answer is '7.460094e+003 lbf' just like I wanted it to, but then it printed 7.4601e+003 the same line. Is it possible to get rid of the second variable display using this method?
Karan Gill
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
Damien Fernando 2019 年 9 月 28 日
Great answer, ty
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 日

0 投票

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.

サインインしてコメントする。

タグ

質問済み:

2011 年 12 月 3 日

コメント済み:

2021 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by