필터 지우기
필터 지우기

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

조회 수: 93 (최근 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.
  댓글 수: 2
Tsvetan Donov
Tsvetan Donov 2016년 5월 27일
Thank you very much! This does help.
Karan Gill
Karan Gill 2017년 4월 7일
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.

댓글을 달려면 로그인하십시오.


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?
Tsvetan Donov
Tsvetan Donov 2016년 5월 27일
Ok I see, so there is no direct and easy way of doing such a thing.

댓글을 달려면 로그인하십시오.


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