How to add units to a number and differentiate between different inputs
128 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tsvetan Donov
am 27 Mai 2016
Kommentiert: Benjamin D'Anjou
am 29 Jul. 2021
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!
0 Kommentare
Akzeptierte Antwort
Karan Gill
am 13 Mär. 2017
Bearbeitet: Karan Gill
am 17 Okt. 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 Kommentar
Benjamin D'Anjou
am 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.
Weitere Antworten (4)
KSSV
am 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 Kommentare
Karan Gill
am 7 Apr. 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.
John D'Errico
am 27 Mai 2016
Bearbeitet: John D'Errico
am 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 Kommentare
John D'Errico
am 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?
Mathieu
am 27 Mai 2016
Bearbeitet: Mathieu
am 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)
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!