unit conversion with dimension check


Updated 10 Sep 2017

View License

converts between different units, and checks that their dimensions are consistent
units may be compounds of other units (e.g. Newton-meters or meters/second)
units may have powers (e.g. meter^3 or m/s2)
units may have scaling prefixes (e.g. milimeters or kW)
unit('N-m','kg.m2/s2') %torque
unit('Pa','N/in2') %pressure
unit('m','s') %causes error because wrong dimensions

Cite As

Christopher Pedersen (2023). unit(varargin) (https://www.mathworks.com/matlabcentral/fileexchange/46045-unit-varargin), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Dates and Time in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Bug fixed: negative prefixes became positive (e.g. mm became 1E3 m rather than 1E-3m).
Added units for magnetic fields: weber, siemen, tesla, gauss

Added handling of nondimensional units.
Added units 'slug','kph' and 'henry'
Added 'reltemp' option to supress warning when converting temperatures
under-the-hood changes to how unit terms are handled.
Additional help on units and options

added: allows nondimensional units
fixed: was not parsing 'mm' (milimeters) correctly
added: error message when cannot parse the power of a term

added 'detail' option that shows how each term of each unit is parsed

'verb' option gives additional reporting at each step (scale and power)
fixed 'verb' reporting : was misreporting original scale (calculation was okay)

Corrected unit pound (weight) - was missing the last digit
fixed error when handling unrecognised units

Added definitions of Calorie and FoodCalorie
Ounce definition was 1000 times too small, fixed.

Added additional units (lightSpeed, horsepower, etc).
Fixed spelling of celsius unit
Divisor "/" now affect only the single following term, rather than all following terms.
Additional reporting of units grouped by type, using "unit report".

Fixed misspelling of unit "celsius"

Added ability to use custom units.

Added absolute temperature conversion.
Fixed bug where prefix+symbol name not recognised (e.g. kohm or mvolt

New: allows a single input instead of two. Does not perform a conversion, instead just reports that one unit.
New: Reports base units of each input