Display a variable and its units
Afficher commentaires plus anciens
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
le 7 Mai 2021
fprintf('The answer is %.2f ft/lb.\n', x)
Réponse acceptée
Plus de réponses (3)
Thomas Green
le 22 Fév 2018
Modifié(e) : Thomas Green
le 22 Fév 2018
16 votes
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
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
Ryan
le 3 Déc 2011
Karan Gill
le 7 Avr 2017
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
le 28 Sep 2019
Great answer, ty
Samuel Katongole
le 19 Mar 2020
Wow..this by Sven works...
Rakesh Chaudhary
le 23 Jan 2021
good..thanks
Ryan
le 3 Déc 2011
0 votes
1 commentaire
Michael Darwish
le 12 Déc 2021
Thank you, that is exactly what I was looking for.
Catégories
En savoir plus sur Code Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!