Effacer les filtres
Effacer les filtres

How to add units to a number and differentiate between different inputs

85 vues (au cours des 30 derniers jours)
Hi guys I am new to this community. I graduated civil engineering 2 weeks ago and I want to start and automating my work a bit. For that I wanted to learn Matlab. I bought couple of books with examples and tutorials that I can follow, however I have couple of questions that I can't find answer to. Lets say I have a variable - the depth of a beam is 300mm. How can I make Matlab recognise that the unit is "mm". I want to try a simple example such as 2m*300mm = "the answer in cm". I did the second moment of area (b*d^3)/12 so I want to assign b and d to me in mm how can i do this?
I have also learned the input function so I can write input=('Enter width: \n'), so that I can write the program so it asks me for breadth and then once again input=('Enter depth: \n'), and then i can write I=(b*d^3)/12 but I don't know how to assign that the first input is the b and the second input is d.
Pease help!

Réponse acceptée

Karan Gill
Karan Gill le 13 Mar 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
  1 commentaire
Benjamin D'Anjou
Benjamin D'Anjou le 29 Juil 2021
Very cool. I'm curious if there is an 'pure' unit that I can use for pure numbers. I'm looking through the list and there doesn't seem to be one. Of course, I can just use the 'sym' function, but conceptually it would be better if there was a pure unit.

Connectez-vous pour commenter.

Plus de réponses (4)

KSSV
KSSV le 27 Mai 2016
use the following to store the variables as you desire.
b = input('Enter width:') ;
d = input('Enter depth:') ;
Coming about units, it is the user responsibility to keep all of them in the same units.
  2 commentaires
Tsvetan Donov
Tsvetan Donov le 27 Mai 2016
Thank you very much! This does help.
Karan Gill
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.

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 27 Mai 2016
Modifié(e) : John D'Errico le 27 Mai 2016
You can't easily use units in MATLAB. Ok, you can do some tricks with the symbolic toolbox. Or, there is even a units tool that I recall seeing on the file exchange. In fact, long ago I recall Cleve Moler toyed with them in a long ago blog as a demo of class programming when classes first came out. (Which shows you how long ago that was.)
While you might like the idea, it would be cumbersome to apply for bigger problems. Worse, it would slow down your computations by a huge amount. One virtue of MATLAB is it can do huge computations quite quickly. This is something that IMHO, has always distinguished it from tools like MATHCAD (a tool that I used for a few years, but found far less useful and less powerful than MATLAB.) Apply units, and you would be killing one of the best features of MATLAB.
So, yes, you can use units on variables. But sorry, while units might be a nice crutch, they are best left to pencil and paper, or the help for your functions. When you DO write the help of course, units are extremely important. Units can be extremely valuable to help understand a problem when you derive a formula.
  3 commentaires
John D'Errico
John D'Errico le 27 Mai 2016
Exactly. Units are great with pencil and paper. I use them all the time when deriving a formula. They help your thinking. But for actual computations, they are a crutch.
How fast can you run using crutches?
Tsvetan Donov
Tsvetan Donov le 27 Mai 2016
Ok I see, so there is no direct and easy way of doing such a thing.

Connectez-vous pour commenter.


Mathieu
Mathieu le 27 Mai 2016
Modifié(e) : Mathieu le 27 Mai 2016
Hello!
For the story of input, take a look at the function inputdlg of MATLAB ;) You will be able to ask for multiple input of the user.
For your first question, I have no simple idea (maybe use struct with a field number and another name unit and you test the filed unit to know the real value)

Jan
Jan le 13 Mar 2017

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by