MATLAB Examples

## Contents

```function varargout = unit_conversion( varargin ) %Conversion factors for two units % ------------------------------------------------------------------------- % Usage: % out = unit_conversion( in, unit1, unit2 ) % in - value in unit1 % out - value in unit2 % unit1/unit2 - two strings with units % types = unit_conversion( 'supported types' ) % types - cell string array with list of supported classes of units % units = unit_conversion( type ) % type - a string that is a member of types % units - cell string array with list of supported units in class % [inv,gain,offset] = unit_conversion( unit1, unit2 ) % unit1/unit2 - two strings with units % inv - false for {gain*val+offset}, true for {gain/val+offset} % gain/offset - constants for equation % % Description: % unit_conversion is both a function for converting units and supporting % GUIs that convert units % % Examples: % unit_conversion('supported types') % unit_conversion(37,'degC','degF') % [inv,gain,offset] = unit_conversion('degC','degF') % % ------------------------------------------------------------------------- % Pieced together by Bob Kagy % Conversion factors gathered from a variety of unverified places, % including other unit conversion scripts, http://www.convert-me.com/, and % http://en.wikipedia.org/ % Switch based on inputs switch nargin case 1 % User is querying for what is supported type = varargin{1}; data = iDatabase; if ischar(type) if strcmpi(type,'supported types') if nargout > 1 error('Incorrect usage - ''supported types'' returns only one output, but %d asked for',nargout); elseif nargout == 1 varargout = {fieldnames(data)}; else % Display available types help(mfilename); disp('Supported Types:'); flds = fieldnames(data); for idxF = 1:length(flds) iDisplayTypes(data,flds{idxF}); end end return; elseif strcmpi(type,'self test') iSelfTest; return; elseif isfield(data,type) if nargout == 1 % Compile list of available types si = data.SIPrefixes; unitArr = [data.(type)(:).unit]; siArr = [data.(type)(:).isSI]; units = cell((length(si)-1)*length(unitArr),1); idxU = 0; for idxA = 1:length(unitArr) idxU = idxU + 1; units(idxU) = unitArr(idxA); if siArr(idxA) > 0 for idxS = 1:length(si) if si(idxS).gain ~= 1 idxU = idxU + 1; units{idxU} = [si(idxS).unit{siArr(idxA)},unitArr{idxA}]; end end end end units = units(1:idxU); varargout = {units}; return; else % Display available types iDisplayTypes(data,type); end return elseif isempty(type) error('Incorrect usage - when passing 1 input must be a string with the name of a type or ''supported types'', was of empty string'); else error('''%s'' is not a supported type of unit',type); end else error('Incorrect usage - when passing 1 input must be a string with the name of a type or ''supported types'', was of class ''%s''',class(type)); end case 2 % User is querying constants to a conversion formula unit1 = varargin{1}; unit2 = varargin{2}; if ischar(unit1) && ischar(unit2) if ~isempty(unit1) && ~isempty(unit2) if ~strcmpi(unit1,unit2) [inv,gain,bias] = iGetConstants(unit1,unit2); else inv = false; gain = 1; bias = 0; end varargout = {inv,gain,bias}; return; else error('Incorrect usage - when passing 2 inputs must both be strings with the name of units, at least one string was empty'); end else error('Incorrect usage - when passing 2 inputs must both be strings with the name of units, first input was of class ''%s'' and second input was of class ''%s''',class(unit1),class(unit2)); end case 3 % User is asking for a conversion val1 = varargin{1}; unit1 = varargin{2}; unit2 = varargin{3}; if ~isnumeric(val1) error('Incorrect usage - when passing 3 inputs first input must be numeric, first inputs was of class ''%s''',class(val1)); elseif ~ischar(unit1) error('Incorrect usage - when passing 3 inputs second input must be string with unit, second input was of class ''%s''',class(unit1)); elseif ~ischar(unit2) error('Incorrect usage - when passing 3 inputs third input must be string with unit, third input was of class ''%s''',class(unit2)); elseif isempty(unit1) error('Incorrect usage - when passing 3 inputs second input must be string with unit, second input was empty string'); elseif isempty(unit1) error('Incorrect usage - when passing 3 inputs third input must be string with unit, third input was empty string'); end % Get gain/offset if ~strcmpi(unit1,unit2) [inv,gain,bias] = iGetConstants(unit1,unit2); else inv = false; gain = 1; bias = 0; end % Calculate output if inv val2 = gain ./ val1 + bias; else val2 = gain .* val1 + bias; end varargout = {val2}; return; otherwise error('Incorrect usage - %d inputs not supported',nargin); end % Incorrect usage error('Incorrect usage - see help for supported inputs'); function iDisplayTypes(data,fld) % Display the list of types on the screen arr = data.(fld); si = data.SIPrefixes; str = sprintf('Type %s:',fld); cLine = ' '; for idxR = 1:length(arr) for idxU = 1:length(arr(idxR).unit) cUnit = arr(idxR).unit{idxU}; if (length(cLine) + 1 + length(cUnit)) > 60 str = sprintf('%s\n%s',str,cLine); cLine = ' '; end cLine = sprintf('%s %s',cLine,cUnit); if arr(idxR).isSI(idxU) ~= 0 dSI = arr(idxR).isSI(idxU); for idxS = 1:length(si) if si(idxS).unit{2} ~= '1' sUnit = [si(idxS).unit{dSI},cUnit]; if (length(cLine) + 1 + length(sUnit)) > 60 str = sprintf('%s\n%s',str,cLine); cLine = ' '; end cLine = sprintf('%s %s',cLine,sUnit); end end end end end if ~strcmp(cLine,' ') str = sprintf('%s\n%s',str,cLine); end disp(str); ```

## ------------------------------------------------------------------------

```function [inv,gain,bias] = iGetConstants(unit1,unit2) % Return the constants to the formula: % y {unit2} = gain * x {unit1} + bias % Find records [rec1,rec2] = iGetRecords(unit1,unit2); % Calculate outputs % inv is xor(inv1,inv2) inv = xor(rec1.inv,rec2.inv); % Gain is gain1 / gain2 gain = rec1.gain1 / rec2.gain1 * 10^(rec1.gain2-rec2.gain2); % Offset is gain2 * offset1 - offset2 bias = gain * rec1.bias - rec2.bias; function [rec1,rec2] = iGetRecords(unit1,unit2) % Return the two records from the same type that have the requested unit % Find all instances of unit1 & unit2 recs1 = iGetRecord(unit1); recs2 = iGetRecord(unit2); % Loop until we find matching units rec1 = []; rec2 = []; for idx1 = 1:length(recs1) for idx2 = 1:length(recs2) if strcmp(recs1(idx1).type,recs2(idx2).type) rec1 = recs1(idx1); rec2 = recs2(idx2); break; end end if ~isempty(rec1) break; end end if isempty(rec1) || isempty(rec2) error('Unable to find units ''%s'' and ''%s'' of the same type - can''t convert',unit1,unit2); end function recs = iGetRecord(unit) % Find all records with the specified units % Get data data = iDatabase; flds = fieldnames(data); si = data.SIPrefixes; % Preallocate arrays idxO = 0; tmp = cell(1,length(flds)); recs = struct('unit',tmp,'isSI',tmp,'gain1',tmp,'gain2',tmp,'bias',tmp,'inv',tmp,'type',tmp); % Find all matching units for idxF=1:length(flds) found = false; for idxR=1:length(data.(flds{idxF})) dataR = data.(flds{idxF})(idxR); for idxU=1:length(dataR.unit) if strcmpi(unit,dataR.unit{idxU}) idxO = idxO + 1; recs(idxO) = struct(... 'unit', {dataR.unit},... 'isSI', dataR.isSI ,... 'gain1', dataR.gain ,... 'gain2', 0 ,... 'bias', dataR.bias ,... 'inv', dataR.inv ,... 'type', flds{idxF} ); found = true; break; elseif dataR.isSI(idxU) > 0 && ... strncmpi(iRev(unit),iRev(dataR.unit{idxU}),length(dataR.unit{idxU})) % Might match an SI unit for idxS = 1:length(si) tstUnit = [si(idxS).unit{dataR.isSI(idxU)},dataR.unit{idxU}]; if (si(idxS).gain ~= 1) && ... strcmpi(unit,tstUnit) % Found a match idxO = idxO + 1; recs(idxO) = struct(... 'unit',{{tstUnit}} ,... 'isSI', 0 ,... 'gain1', dataR.gain ,... 'gain2', log10(si(idxS).gain),... 'bias', dataR.bias ,... 'inv', dataR.inv ,... 'type', flds{idxF} ); found = true; break; end end end end if found, break; end end end if idxO > 0 recs = recs(1:idxO); else error('Unable to find unit ''%s''',unit); end function s = iRev(s) % Reverse a string s = s(end:-1:1); ```

## ------------------------------------------------------------------------

```function iSelfTest % Perform self tests to ensure proper operation. One of the most important % bits is to insure we don't inadvertantly create duplicate units in one % class or duplicate pairs in two classes. This isn't going to be fast. % Get types stTime = now; me = str2func(mfilename); types = me('supported types'); % Loop over database to ensure all entries are valid db = iDatabase; flds = fieldnames(db); for idxF=1:length(flds) cDB = db.(flds{idxF}); if ~isstruct(cDB) error('For ''%s'', not a structure',flds{idxF}); elseif ~isempty(setdiff({'unit','isSI','gain','bias'},fieldnames(cDB))) error('Incorrect fields for ''%s''',flds{idxF}); end for idx=1:length(cDB) if ~iscellstr(cDB(idx).unit) error('For %d in %s .unit is not a cell array of strings',idx,flds{idxF}); elseif ~isa(cDB(idx).isSI,'double') error('For %d in %s .isSI is not a double array',idx,flds{idxF}); elseif length(cDB(idx).unit) ~= length(cDB(idx).isSI) error('For %d in %s length(.unit) ~= length(.isSI)',idx,flds{idxF}); elseif ~isa(cDB(idx).gain,'double') || length(cDB(idx).gain) ~= 1 error('For %d in %s .gain is not a scalar double',idx,flds{idxF}); elseif ~isa(cDB(idx).bias,'double') || length(cDB(idx).bias) ~= 1 error('For %d in %s .bias is not a scalar double',idx,flds{idxF}); elseif ~islogical(cDB(idx).inv) || length(cDB(idx).inv) ~= 1 error('For %d in %s .inv is not a scalar boolean',idx,flds{idxF}); end end end % Get units list = struct; for idxF=1:length(types) list.(types{idxF}) = me(types{idxF}); end % Check for uniqueness within each type u = cell(3,1); for idxF=1:length(types) [u{1:3}] = unique(list.(types{idxF})); if length(u{1}) < length(list.(types{idxF})) % ERROR - duplicate units dupes = cell(length(u{3}),1); idxD = 0; for idxU=1:length(u{3}) if sum(u{3}(idxU) == u{3}) > 1 idxD = idxD + 1; dupes{idxD} = list.(types{idxF}){u{3}(idxU)}; end end dupes = unique(dupes); strD = ''; for idxD = 1:length(dupes) strD = sprintf('%s %s',strD,dupes{idxD}); end error('Duplicate units detected in %s:%s',types{idxF},strD); end end % Check for unqueness within each pair % It is fine to have m for meter and milli, as long as we don't also have % ft for feet and 10^-7 pairs = struct; for idxF=1:length(types) units = list.(types{idxF}); pair = cell(length(units).^2,1); idxP = 0; for idx1=1:length(units) for idx2=1:length(units) if idx1 ~= idx2 idxP = idxP + 1; pair{idxP} = [units{idx1},' - ',units{idx2}]; end end end pairs.(types{idxF}) = pair(1:idxP); end for idxF1=1:length(types) for idxF2=(idxF1+1):length(types) check = ismember(pairs.(types{idxF1}),pairs.(types{idxF2})); if any(check) % ERROR duplicate pairs str = sprintf('Pairs of units appear in two classes, %s & %s:',... types{idxF1},types{idxF2}); idx = find(check); for idxC=1:length(idx) str = sprintf('%s\n\t%s',str,pairs.(types{idxF1}){idx(idxC)}); end error(str); end end end % Check for commutability maxErr = {0,'';0,''}; constants = cell(1,2); for idxF=1:length(types) units = list.(types{idxF}); for idx1=1:length(units) for idx2=1:length(units) try val = unit_conversion(... unit_conversion(pi,units{idx1},units{idx2}),... units{idx2},units{idx1}); catch e = lasterror; error('Error checking commutability of ''%s'' & ''%s'':\n%d : %s : %s\n%s',... units{idx1},units{idx2},e.stack(1).line,e.stack(1).name,e.stack(1).file,... e.message); end [constants{1:2}] = unit_conversion(units{idx1},units{idx2}); if constants{2} == 0 row = 1; else row = 2; end if abs(val-pi) > maxErr{row,1} maxErr{row,1} = abs(val-pi); maxErr{row,2} = [units{idx1},' - ',units{idx2}]; end end end end % SUCCESS disp(sprintf('%s %s\n%s ''%s'' : %s\n%s ''%s'' : %s',... 'Self test successful - Elapsed time ',datestr(now-stTime,'MM:SS.FFF'),... 'Max error in commutability test (zero bias) occurred with',maxErr{1,2},maxErr{1,1},... 'Max error in commutability test (with bias) occurred with',maxErr{2,2},maxErr{2,1})); ```

## ------------------------------------------------------------------------

```function data = iDatabase % Database of information about what can be converted % data - structure with field for each class % .(type) - array structure with element for each unit % .unit - cell string array of alternative names for unit % .isSI - logical array of whether each name can use SI Prefixes % .gain - double with constant in formula {common = gain * this + bias} % .bias - double with constant in formula {common = gain * this + bias} % .inv - whether to invert x in the formula {common = gain \ this + bias} persistent dataP if isempty(dataP) dataP = struct(... 'SIPrefixes' , iDB_SIPrefixes ,... 'Length' , iDB_Length ,... 'Temperature' , iDB_Temperature ,... 'Time' , iDB_Time ,... 'Mass' , iDB_Mass ,... 'Force' , iDB_Force ,... 'Pressure' , iDB_Pressure ,... 'WorkEnergyTorque' , iDB_WorkEnergyTorque,... 'Power' , iDB_Power ,... 'Velocity' , iDB_Velocity ,... 'Acceleration' , iDB_Acceleration ,... 'Area' , iDB_Area ,... 'Volume' , iDB_Volume ,... 'Density' , iDB_Density ,... 'MassMomentOfIntertia' , iDB_MassIntertia ,... 'AreaMomentOfIntertia' , iDB_AreaIntertia ,... 'Frequency' , iDB_Frequency ,... 'Angle' , iDB_Angle ,... 'StressIntensityFactor', iDB_SIF ,... 'Viscosity' , iDB_Viscosity ,... 'Strain' , iDB_Strain ,... 'FuelConsumption' , iDB_FuelConsumption ... ); end data = dataP; function data = iDB_SIPrefixes % Database of information about SI Prefixes % http://physics.nist.gov/cuu/Units/prefixes.html % These are also used to calculate additional names data = [struct(... 'unit',{{'yotta','Y'}},... 'isSI',[0,0],... 'gain',1e24,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'zetta','Z'}},... 'isSI',[0,0],... 'gain',1e21,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'exa','E'}},... 'isSI',[0,0],... 'gain',1e18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'peta','P'}},... 'isSI',[0,0],... 'gain',1e15,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'tera','T'}},... 'isSI',[0,0],... 'gain',1e12,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'giga','G'}},... 'isSI',[0,0],... 'gain',1e9,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mega','M'}},... 'isSI',[0,0],... 'gain',1e6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kilo','k'}},... 'isSI',[0,0],... 'gain',1e3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'hecto','h'}},... 'isSI',[0,0],... 'gain',1e2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'deka','da'}},... 'isSI',[0,0],... 'gain',1e1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'one','1'}},... 'isSI',[0,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'deci','d'}},... 'isSI',[0,0],... 'gain',1e-1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'centi','c'}},... 'isSI',[0,0],... 'gain',1e-2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'milli','m'}},... 'isSI',[0,0],... 'gain',1e-3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'micro','µ'}},... 'isSI',[0,0],... 'gain',1e-6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'nano','n'}},... 'isSI',[0,0],... 'gain',1e-9,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'pico','p'}},... 'isSI',[0,0],... 'gain',1e-12,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'femto','f'}},... 'isSI',[0,0],... 'gain',1e-15,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'atto','a'}},... 'isSI',[0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'zepto','z'}},... 'isSI',[0,0],... 'gain',1e-21,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'yocto','y'}},... 'isSI',[0,0],... 'gain',1e-24,... 'bias',0,... 'inv',false)]; function data = iDB_Length % Database of information about lengths % The common unit is the meter data = [struct(... 'unit',{{'meter','m'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Angstrom'}},... 'isSI',0,... 'gain',1e-10,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'league'}},... 'isSI',0,... 'gain',4828.032,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mile','mi'}},... 'isSI',[0,0],... 'gain',1609.344,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'furlong'}},... 'isSI',0,... 'gain',201.168,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'chain'}},... 'isSI',0,... 'gain',20.1168,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'rod','rd'}},... 'isSI',[0,0],... 'gain',5.0292,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'perch'}},... 'isSI',0,... 'gain',5.0292,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'yard','yd'}},... 'isSI',[0,0],... 'gain',0.9144,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'foot','ft'}},... 'isSI',[0,0],... 'gain',0.3048,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'link'}},... 'isSI',0,... 'gain',0.201168,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'hand'}},... 'isSI',0,... 'gain',0.1016,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'inch'}},... 'isSI',0,... 'gain',0.0254,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'line'}},... 'isSI',0,... 'gain',0.002116667,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mil'}},... 'isSI',0,... 'gain',2.54e-5,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'sea league'}},... 'isSI',0,... 'gain',5556,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'sea mile'}},... 'isSI',0,... 'gain',1852,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'fathom'}},... 'isSI',0,... 'gain',1.8288,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'parsec','pc'}},... 'isSI',[0,0],... 'gain',3.262*9.46075309081898e15,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'light year'}},... 'isSI',0,... 'gain',9.46075309081898e15,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'astronomical unit','au'}},... 'isSI',[0,0],... 'gain',1.495979e11,... 'bias',0,... 'inv',false) ]; function data = iDB_Temperature % Database of information about temperature % The common unit is the degrees Kelvin data = [struct(... 'unit',{{'kelvin','degK'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Celsius','degC'}},... 'isSI',[1,2],... 'gain',1,... 'bias',273.15,... 'inv',false),... struct(... 'unit',{{'Fahrenheit','degF'}},... 'isSI',[0,0],... 'gain',5/9,... % kelvin = 5/9 * ( degF - 32 ) + 273.15 = 5/9 * degF - 5/9 * 32 + 273.15 'bias',(5/9)*(-32)+273.15,... 'inv',false),... struct(... 'unit',{{'Rankine','degRa'}},... 'isSI',[0,0],... 'gain',5/9,... 'bias',0,... 'inv',false)]; function data = iDB_Time % Database of information about time % The common unit is the second data = [struct(... 'unit',{{'second','s','sec'}},... 'isSI',[1,2,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'minute','min'}},... 'isSI',[0,0],... 'gain',60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'hour','hr'}},... 'isSI',[0,0],... 'gain',60*60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'day'}},... 'isSI',0,... 'gain',24*60*60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'week','wk'}},... 'isSI',[0,0],... 'gain',7*24*60*60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Gregorian year','year'}},... 'isSI',[0,0],... 'gain',365.2422*24*60*60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'century'}},... 'isSI',0,... 'gain',100*365.2422*24*60*60,... 'bias',0,... 'inv',false)]; function data = iDB_Mass % Database of information about mass % The common unit is the kilogram data = [struct(... 'unit',{{'gram','g'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'tonne','t'}},... 'isSI',[0,0],... 'gain',1000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'short ton','tUS'}},... 'isSI',[0,0],... 'gain',907.1847,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'long ton','tUK'}},... 'isSI',[0,0],... 'gain',1016.047,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'pound','lbs','lb'}},... 'isSI',[0,0,0],... 'gain',0.4535924,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ounce','oz'}},... 'isSI',[0,0],... 'gain',0.02834952,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'troy ounce'}},... 'isSI',0,... 'gain',0.03110348,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'atomic mass unit','amu'}},... 'isSI',[0,0],... 'gain',1.66044e-27,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'stone'}},... 'isSI',0,... 'gain',6.350293,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'dram','dr'}},... 'isSI',[0,0],... 'gain',0.001771845,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'carat'}},... 'isSI',0,... 'gain',0.0002,... 'bias',0,... 'inv',false)]; function data = iDB_Force % Database of information about force % The common unit is Newtons % http://www.sengpielaudio.com/ConvForce.htm data = [struct(... 'unit',{{'newton','N'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'dyne','dyn'}},... 'isSI',[0,0],... 'gain',1e-5,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kip','kip-force','kipf','klbf'}},... 'isSI',[0,0,0,0],... 'gain',4448.222,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'pound-force','lbf'}},... 'isSI',[0,0],... 'gain',4.448222,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ounce-force','avoirdupois-force','ozf'}},... 'isSI',[0,0,0],... 'gain',0.2780139,... 'bias',0,... 'inv',false)]; function data = iDB_Pressure % Database of information about pressure and stress % The common unit is Pascal data = [struct(... 'unit',{{'pascal','Pa'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'N/mm^2'}},... 'isSI',0,... 'gain',1e6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'bar'}},... 'isSI',0,... 'gain',1e5,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'millibar','mbar'}},... 'isSI',[0,0],... 'gain',100,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'dyne/cm^2'}},... 'isSI',0,... 'gain',0.1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'physical atmosphere','atmosphere','atm'}},... 'isSI',[0,0,0],... 'gain',101325,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'technical atmposhere'}},... 'isSI',0,... 'gain',98066.5,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'thousand pounds per square inch','ksi'}},... 'isSI',[0,0],... 'gain',6894757,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'pounds per square inch','psi'}},... 'isSI',[0,0],... 'gain',6894.757,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'millimeter of mercury','mmHg','torr'}},... 'isSI',[0,0,0],... 'gain',133.322,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'inch of mercury','inHg'}},... 'isSI',[0,0],... 'gain',3386.379,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'millimeter of water','mmH2O'}},... 'isSI',[0,0],... 'gain',9.80665,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'centimeter of water','cmH2O'}},... 'isSI',[0,0],... 'gain',98.06379,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'inch of water','inH2O'}},... 'isSI',[0,0],... 'gain',249.082,... 'bias',0,... 'inv',false)]; function data = iDB_WorkEnergyTorque % Database of information about work, energy, and torque % The common unit is joule data = [struct(... 'unit',{{'joule','J','Nm'}},... 'isSI',[1,2,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Ncm'}},... 'isSI',0,... 'gain',1/100,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Nmm'}},... 'isSI',0,... 'gain',1/1000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'lb-ft'}},... 'isSI',0,... 'gain',1.3558179483314004,... % Wikipedia: Foot-pound force 'bias',0,... 'inv',false),... struct(... 'unit',{{'lb-in'}},... 'isSI',0,... 'gain',0.112984833,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'oz-in'}},... 'isSI',0,... 'gain',7.061552e-3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'British thermal unit','Btu'}},... 'isSI',[0,0],... 'gain',1055.056,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'watt hour','Wh'}},... 'isSI',[1,2],... 'gain',3600,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'watt-second','Ws'}},... 'isSI',[0,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'International Table calorie','calorie','cal'}},... 'isSI',[0,1,2],... 'gain',4.1868,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'thermochemical calorie'}},... 'isSI',0,... 'gain',4.184,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'electronvolts','eV'}},... 'isSI',[0,2],... 'gain',1.6021735314e-19,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'therm'}},... 'isSI',0,... 'gain',1.05506e8,... 'bias',0,... 'inv',false)]; function data = iDB_Power % Database of information about Power % The common unit is watt data = [struct(... 'unit',{{'watt','W','J/s'}},... 'isSI',[1,2,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'hp'}},... 'isSI',0,... 'gain',745.6999,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Btu/h'}},... 'isSI',0,... 'gain',0.2930711,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Btu/s'}},... 'isSI',0,... 'gain',1055.056,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kcal/h'}},... 'isSI',0,... 'gain',1.163,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'J/h'}},... 'isSI',0,... 'gain',1/3600,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'erg per second','erg/s'}},... 'isSI',[0,0],... 'gain',1e-7,... 'bias',0,... 'inv',false)]; function data = iDB_Velocity % Database of information about velocity % The common unit is m/s data = [struct(... 'unit',{{'m/s'}},... 'isSI',2,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'km/h','km/hr'}},... 'isSI',[0,0],... 'gain',1000/3600,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'m/min'}},... 'isSI',2,... 'gain',1/60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mph'}},... 'isSI',0,... 'gain',1609.344/3600,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mps'}},... 'isSI',0,... 'gain',1609.344,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ft/s'}},... 'isSI',0,... 'gain',0.3048,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'in/s'}},... 'isSI',0,... 'gain',0.0254,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ft/min'}},... 'isSI',0,... 'gain',0.3048/60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'in/min'}},... 'isSI',0,... 'gain',0.0254/60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Mach'}},... 'isSI',0,... 'gain',331.5,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'knots'}},... 'isSI',0,... 'gain',0.514444,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'speed of light in vacuum'}},... 'isSI',0,... 'gain',2.997925e8,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'s/km'}},... 'isSI',0,... 'gain',1000,... 'bias',0,... 'inv',true),... struct(... 'unit',{{'s/mile'}},... 'isSI',0,... 'gain',1609.344,... 'bias',0,... 'inv',true)]; function data = iDB_Acceleration % Database of information about acceleration % The common unit is m/s^2 data = [struct(... 'unit',{{'m/s^2'}},... 'isSI',2,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'G'}},... 'isSI',0,... 'gain',9.80665,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ft/s^2'}},... 'isSI',0,... 'gain',0.3048,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'Galileo'}},... 'isSI',0,... 'gain',0.1,... 'bias',0,... 'inv',false)]; function data = iDB_Area % Database of information about area % The common unit is square meter data = [struct(... 'unit',{{'square meter','m^2'}},... 'isSI',[0,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square kilometer','km^2'}},... 'isSI',[0,0],... 'gain',1000000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square centimeter','cm^2'}},... 'isSI',[0,0],... 'gain',1e-4,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square millimeter','mm^2'}},... 'isSI',[0,0],... 'gain',1e-6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square micrometer','µm^2'}},... 'isSI',[0,0],... 'gain',1e-12,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square mile','sq-mile'}},... 'isSI',[0,0],... 'gain',1609.344^2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square yard','sq-yd'}},... 'isSI',[0,0],... 'gain',0.83612736,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square foot','sq-ft'}},... 'isSI',[0,0],... 'gain',0.3048^2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'square inch','sq-in'}},... 'isSI',[0,0],... 'gain',0.0254^2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'acre'}},... 'isSI',0,... 'gain',4046.8564224,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'are','a','ar'}},... 'isSI',[1,0,0],... 'gain',100,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'barn','b'}},... 'isSI',[0,0],... 'gain',1e-28,... 'bias',0,... 'inv',false)]; function data = iDB_Volume % Database of information about volume % The common unit is cubic meter % There are many more, but I don't know an easy way to distinguish the mess % that is liquid -vs- dry measurements data = [struct(... 'unit',{{'cubic meter','m^3'}},... 'isSI',[0,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic kilometer','km^3'}},... 'isSI',[0,0],... 'gain',1e9,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic centimeter','cm^3','cc'}},... 'isSI',[0,0,0],... 'gain',1e-6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic millimeter','mm^3'}},... 'isSI',[0,0],... 'gain',1e-9,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic micrometer','µm^3'}},... 'isSI',[0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic mile','cu-mile','mile^3'}},... 'isSI',[0,0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic yard','cu-yard','yard^3','cu-yd','yd^3'}},... 'isSI',[0,0,0,0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic foot','cu-foot','foot^3','cu-ft','ft^3'}},... 'isSI',[0,0,0,0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cubic inch','cu-inch','inch^3','cu-in','in^3'}},... 'isSI',[0,0,0,0,0],... 'gain',1e-18,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'liter','l'}},... 'isSI',[1,2],... 'gain',1/1000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'gallon US - liquid','galUS'}},... 'isSI',[0,0],... 'gain',3.785412e-3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'gallon US - dry'}},... 'isSI',0,... 'gain',0.004404884,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'gallon UK'}},... 'isSI',0,... 'gain',4.546099e-3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'acre foot'}},... 'isSI',0,... 'gain',1233.482,... 'bias',0,... 'inv',false)]; function data = iDB_Density % Database of information about density % The common unit is kg/m^3 data = [struct(... 'unit',{{'kg/m^3'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'g/cm^3'}},... 'isSI',0,... 'gain',1000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'g/mm^3'}},... 'isSI',0,... 'gain',1e6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kg/l'}},... 'isSI',0,... 'gain',1000,... 'bias',0,... 'inv',false)]; function data = iDB_MassIntertia % Database of information about mass moment of intertia % The common unit is kg*m^2 data = [struct(... 'unit',{{'kg*m^2'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kg*cm^2'}},... 'isSI',0,... 'gain',1e-4,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'kg*mm^2'}},... 'isSI',0,... 'gain',1e-6,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'g*m^2'}},... 'isSI',0,... 'gain',1/1000,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'g*cm^2'}},... 'isSI',0,... 'gain',1e-7,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'g*mm^2'}},... 'isSI',0,... 'gain',1e-9,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'lb*ft^2'}},... 'isSI',0,... 'gain',0.04214011,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'in*ft^2'}},... 'isSI',0,... 'gain',0.2926397e-3,... 'bias',0,... 'inv',false)]; function data = iDB_AreaIntertia % Database of information about area moment of intertia % The common unit is m^4 data = [struct(... 'unit',{{'m^4'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'cm^4'}},... 'isSI',0,... 'gain',1e-8,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mm^4'}},... 'isSI',0,... 'gain',1e-12,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ft^4'}},... 'isSI',0,... 'gain',1/(0.3048^4),... 'bias',0,... 'inv',false),... struct(... 'unit',{{'in^4'}},... 'isSI',0,... 'gain',1/(0.0254^4),... 'bias',0,... 'inv',false)]; function data = iDB_Frequency % Database of information about frequency / angular velocity % The common unit is Hertz data = [struct(... 'unit',{{'Hertz','Hz'}},... 'isSI',[1,2],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'1/min','rpm'}},... 'isSI',[0,0],... 'gain',1/60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'1/hr','1/hour'}},... 'isSI',[0,0],... 'gain',1/3600,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'rad/s','rad/sec'}},... 'isSI',[0,0],... 'gain',1/(2*pi),... 'bias',0,... 'inv',false),... struct(... 'unit',{{'deg/s','deg/sec'}},... 'isSI',[0,0],... 'gain',1/360,... 'bias',0,... 'inv',false)]; function data = iDB_Angle % Database of information about angle % The common unit is radians data = [struct(... 'unit',{{'rad','radian'}},... 'isSI',[0,0],... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'degree','deg'}},... 'isSI',[0,0],... 'gain',pi/180,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'rev','circumference'}},... 'isSI',[0,0],... 'gain',2*pi,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'grade','grad'}},... 'isSI',[0,0],... 'gain',0.9*pi/180,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'angle - minute'}},... 'isSI',0,... 'gain',(pi/180)/60,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'angle - second'}},... 'isSI',0,... 'gain',(pi/180)/3600,... 'bias',0,... 'inv',false)]; function data = iDB_SIF % Database of information about Stress Intensity Factor % The common unit is MPa*m^1/2 data = [struct(... 'unit',{{'MPa*m^1/2'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'MPa*mm^1/2'}},... 'isSI',0,... 'gain',1/sqrt(1000),... 'bias',0,... 'inv',false),... struct(... 'unit',{{'ksi*in^1/2'}},... 'isSI',0,... 'gain',6.894757.*sqrt(0.0254),... 'bias',0,... 'inv',false),... struct(... 'unit',{{'psi*in^1/2'}},... 'isSI',0,... 'gain',6894.757.*sqrt(0.0254),... 'bias',0,... 'inv',false)]; function data = iDB_Viscosity % Database of information about viscosity % The common unit is Ns/m^2 data = [struct(... 'unit',{{'Ns/m^2'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'poise'}},... 'isSI',1,... 'gain',1/10,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'lbfs/sq-ft'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false)]; function data = iDB_Strain % Database of information about strain % The common unit is m/m data = [struct(... 'unit',{{'m/m'}},... 'isSI',2,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'%'}},... 'isSI',0,... 'gain',1e-2,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'o/oo'}},... 'isSI',0,... 'gain',1e-3,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'in/in'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'mill/in'}},... 'isSI',0,... 'gain',1e-3,... 'bias',0,... 'inv',false)]; function data = iDB_FuelConsumption % Database of information about fuel consumption % The common unit is liter / 100km data = [struct(... 'unit',{{'l/100km'}},... 'isSI',0,... 'gain',1,... 'bias',0,... 'inv',false),... struct(... 'unit',{{'km/l'}},... 'isSI',0,... 'gain',1/100,... 'bias',0,... 'inv',true),... struct(... 'unit',{{'miles/galUS'}},... 'isSI',0,... 'gain',235.214596754951,... 'bias',0,... 'inv',true),... struct(... 'unit',{{'galUS/100miles'}},... 'isSI',0,... 'gain',2.352145,... 'bias',0,... 'inv',true)]; ```