diff m-toolbox/classes/@unit/toSI.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/@unit/toSI.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,76 @@
+% toSI converts the units to SI.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: toSI converts the units to SI base units.
+% 
+%       Expand units to be combinations of 
+%         {'m', 'kg', 's', 'A', 'mol', 'cd'}
+%
+% CALL:        u_out = toSI(i_in);
+%
+% VERSION:     $Id: toSI.m,v 1.1 2011/03/07 17:29:57 hewitson Exp $
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function varargout = toSI(vi)
+
+  % process the units and exponents
+  v = unit;  
+  for kk=1:numel(vi.strs)
+    s = unit(siForUnit(vi.strs{kk}));
+    s.exps = vi.exps(kk).*s.exps;
+    v = v.*s;
+  end  
+  v.simplify;
+  
+  % Either modify the input or set the output
+  if nargout == 0
+    vi.strs = v.strs;
+    vi.exps = v.exps;
+    vi.vals = v.vals;
+  else 
+    varargout{1} = v;
+  end
+  
+end
+
+
+function s = siForUnit(u)
+
+  % output == input in default case
+  s = u;
+  switch u
+    case 'rad'
+      s = 'm m^-1';
+    case 'sr'
+      s = 'm^2 m^-2';
+    case 'Hz'
+      s = 's^-1';
+    case 'N'
+      s = 'kg m s^-2';
+    case 'Pa'
+      s = 'm^-1 kg s^-2';
+    case 'J'
+      s = 'kg m^2 s^-2';
+    case 'W'
+      s = 'm^2 kg s^-3';
+    case 'C'
+      s = 's A';
+    case 'V'
+      s = 'm^2 kg s^-3 A^-1';
+    case 'F'
+      s = 'm^-2 kg^-1 s^4 A^2';
+    case 'Ohm'
+      s = 'm^2 kg s^-3 A^-2';
+    case 'S'
+      s = 'm^-2 kg^-1 s^3 A^2';
+    case 'Wb'
+      s = 'm^2 kg s^-2 A^-1';
+    case 'T'
+      s = 'kg s^-2 A^-1';
+    case 'H'
+      s = 'm^2 kg s^-2 A^-2';
+    case 'degC'
+      s = 'K';
+  end
+end