Automatic UNITS conversion

버전 1.0.0.0 (9.85 KB) 작성자: Riccardo Meldolesi
Eliminate UNITS related errors in code, through automatic units conversion
다운로드 수: 792
업데이트 날짜: 2008/2/6

라이선스 없음

Eliminate UNITS related errors in code, through automatic units conversion

A UNIT class is defined to automatically perform units conversion into the MKS system (metre,kg,sec).

I aim to solve the problem of using, say, mm instead of m, or converting mm to inches.
This is also a problem during plotting, because of the need to keep track of the units
of what is being plotted.

For this reason I like to define all variables into the MKS system by writing:

diameter = 10.0 * mm;

This is an automatic conversion into MKS, while still allowing me to think in mm (or grams, N, inches ...)

If the value of mm is 1e-3, then the value of diameter is 1e-2, which, experessed in
metres is = 10 mm. (everything is fine)

From this point onward all the calculations that use 'diameter' can be done in the MKS,
avoiding any "order of magnitude error". The syntax above is also particularly useful in
input files where a lot of scalar parameters are defined. It provides a lot of
information at a glance.

When a variable needs to be displayed or plotted the following syntax can be used:

disp(diameterVector/mm)

or

plot(diameterVector/mm,lengthVector/mm)

Also it is possible to perform units conversions in functions or on the command line.
For example the following statement converts 10 mm into inches

>> 10 * mm/in
ans =
0.3937

all of this is not a my invention, being in use in at least another software. I adopted
it because for me it works very well.

This new submission allows to create composite units, like:

typing: kg/mm^3
will display: kg/(mm^3) = 1.000000e+009

in this case the class returned variable is 'COMPOSITEUNIT'

Other examples:
typing: kg*m/sec^2 / (kg/mm^3)
will display: kg*m/(sec^2)/(kg/(mm^3)) = 1.000000e-009

in this case the class returned variable is 'COMPOSITEUNIT'

or:
kg*m/sec^2
kg*m/(sec^2) = 1.000000

INSTALLATION

To implement the syntax above I created a class called 'unit', which must be saved into
a directory called 'unitClasses'. The directory 'unitClasses' must be added to the
classpath.

The function 'unitClasses\@unit\private\getStructure.m' contains a comprehensive list of
units, which can be extended as suitable.

Then by calling the method 'update(unit)', a number of classes will be created, one for
each of the units defined in 'getStructure'.

You might then have to type:

'clear classes'
'rehash toolbox'

the updating can be done at any time a new unit is added to the function 'unitsList'.
from now on every time a syntax like 'diameter = 10.0 * mm;' is used a unit conversion
is made.

More info in the README.txt file

Your feedback is welcome

인용 양식

Riccardo Meldolesi (2024). Automatic UNITS conversion (https://www.mathworks.com/matlabcentral/fileexchange/18638-automatic-units-conversion), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R14
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Wind Power에 대해 자세히 알아보기
도움

받음: UNITS conversion

Community Treasure Hunt

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

Start Hunting!

unitClasses/@compositeUnit/

unitClasses/@noUnit/

unitClasses/@unit/

버전 게시됨 릴리스 정보
1.0.0.0