Display a variable and its units

71 vues (au cours des 30 derniers jours)
Ryan
Ryan le 3 Déc 2011
Commenté : Michael Darwish le 12 Déc 2021
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 commentaire
Dianne Claire Nocon
Dianne Claire Nocon le 7 Mai 2021
fprintf('The answer is %.2f ft/lb.\n', x)

Connectez-vous pour commenter.

Réponse acceptée

Karan Gill
Karan Gill le 7 Avr 2017
Modifié(e) : Karan Gill le 17 Oct 2017
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 commentaires
Samuel Katongole
Samuel Katongole le 19 Mar 2020
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 le 24 Sep 2020
i am also using fprintf and i need to display my units next to the numbers

Connectez-vous pour commenter.

Plus de réponses (3)

Thomas Green
Thomas Green le 22 Fév 2018
Modifié(e) : Thomas Green le 22 Fév 2018
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 commentaires
Thomas Jeffrey
Thomas Jeffrey le 5 Nov 2018
True that
Medinah Lee
Medinah Lee le 13 Mai 2019
LOL Preach!

Connectez-vous pour commenter.


Sven
Sven le 3 Déc 2011
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 commentaires
Samuel Katongole
Samuel Katongole le 19 Mar 2020
Wow..this by Sven works...
Rakesh Chaudhary
Rakesh Chaudhary le 23 Jan 2021
good..thanks

Connectez-vous pour commenter.


Ryan
Ryan le 3 Déc 2011
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 commentaire
Michael Darwish
Michael Darwish le 12 Déc 2021
Thank you, that is exactly what I was looking for.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by