Code covered by the BSD License  

Highlights from
Unit Conversion

from Unit Conversion by Anthony Kendall
A simple unit conversion function that is easily extended.

[output]=unit_conversions(input,varargin)
function [output]=unit_conversions(input,varargin)
%This function performs unit conversions of any types that have been
%defined below.  
%inputs are: input value(s), input units (see list below), output units
%(see list below).  Both input and output units must be a string from the
%list below.  If output units are not specified, then SI units (with Celsius as temperature) will be
%output by default.
%
%To add new conversion types, modify the code below by specifying:
%'unit',[factor,offset].  The factor and offset values are applied as
%follows: SI_unit =  (input + offset)*factor;
%
%Currently supported units:
%velocity: kts, m/s, mph
%length: ft, in, mm, mi, cm, m, km
%temperature: F, C, K
%pressures: mbar, bar, Pa, inHg
%energy: kJ, J
%angles: deg, rad, az

%define conversion table
table=struct('kts',[0.514444,0],'mps',[1,0],'mph',[0.44704,0],... %velocity group
    'ft',[0.3048,0],'in',[0.0254,0],'mi',[1609.34,0],'mm',[0.001,0],'cm',[0.01,0],'km',[10,0],... %length group
    'F',[5/9,-32],'C',[1,0],'K',[1,-273.15],... %temperature group
    'mbar',[100,0],'bar',[1e5,0],'Pa',[1,0],'inHg',[3386.39,0],... %pressure group
    'kJ',[0.001,0],'J',[1,0],... %energy group
    'deg',[2*pi()/360,0],'rad',[1,0],'az',[-2*pi()/360,-90]); %angular units group

if nargin>1
    input_units=varargin{1};
else
    error('Input units must be specified');
end
intermed=(input+table.(input_units)(2))*table.(input_units)(1);
if nargin>2
    output_units=varargin{2};
    output=intermed/table.(output_units)(1)-table.(output_units)(2);
else
    output=intermed;
end

Contact us at files@mathworks.com