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

175 views (last 30 days)
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!

Accepted Answer

Karan Gill
Karan Gill on 13 Mar 2017
Edited: Karan Gill on 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 Comment
Benjamin D'Anjou
Benjamin D'Anjou on 29 Jul 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.

Sign in to comment.

More Answers (4)

KSSV
KSSV on 27 May 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.

John D'Errico
John D'Errico on 27 May 2016
Edited: John D'Errico on 27 May 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 Comments
John D'Errico
John D'Errico on 27 May 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?

Sign in to comment.


Mathieu
Mathieu on 27 May 2016
Edited: Mathieu on 27 May 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 on 13 Mar 2017

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!