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

89 次查看(过去 30 天)
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!

采纳的回答

Karan Gill
Karan Gill 2017-3-13
编辑:Karan Gill 2017-10-17
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 个评论
Benjamin D'Anjou
Benjamin D'Anjou 2021-7-29
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.

请先登录,再进行评论。

更多回答(4 个)

KSSV
KSSV 2016-5-27
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 2016-5-27
编辑:John D'Errico 2016-5-27
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 个评论
John D'Errico
John D'Errico 2016-5-27
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?

请先登录,再进行评论。


Mathieu
Mathieu 2016-5-27
编辑:Mathieu 2016-5-27
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 2017-3-13

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by