comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % toSI converts the units to SI.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: toSI converts the units to SI base units.
5 %
6 % Expand units to be combinations of
7 % {'m', 'kg', 's', 'A', 'mol', 'cd'}
8 %
9 % CALL: u_out = toSI(i_in);
10 %
11 % VERSION: $Id: toSI.m,v 1.1 2011/03/07 17:29:57 hewitson Exp $
12 %
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15 function varargout = toSI(vi)
16
17 % process the units and exponents
18 v = unit;
19 for kk=1:numel(vi.strs)
20 s = unit(siForUnit(vi.strs{kk}));
21 s.exps = vi.exps(kk).*s.exps;
22 v = v.*s;
23 end
24 v.simplify;
25
26 % Either modify the input or set the output
27 if nargout == 0
28 vi.strs = v.strs;
29 vi.exps = v.exps;
30 vi.vals = v.vals;
31 else
32 varargout{1} = v;
33 end
34
35 end
36
37
38 function s = siForUnit(u)
39
40 % output == input in default case
41 s = u;
42 switch u
43 case 'rad'
44 s = 'm m^-1';
45 case 'sr'
46 s = 'm^2 m^-2';
47 case 'Hz'
48 s = 's^-1';
49 case 'N'
50 s = 'kg m s^-2';
51 case 'Pa'
52 s = 'm^-1 kg s^-2';
53 case 'J'
54 s = 'kg m^2 s^-2';
55 case 'W'
56 s = 'm^2 kg s^-3';
57 case 'C'
58 s = 's A';
59 case 'V'
60 s = 'm^2 kg s^-3 A^-1';
61 case 'F'
62 s = 'm^-2 kg^-1 s^4 A^2';
63 case 'Ohm'
64 s = 'm^2 kg s^-3 A^-2';
65 case 'S'
66 s = 'm^-2 kg^-1 s^3 A^2';
67 case 'Wb'
68 s = 'm^2 kg s^-2 A^-1';
69 case 'T'
70 s = 'kg s^-2 A^-1';
71 case 'H'
72 s = 'm^2 kg s^-2 A^-2';
73 case 'degC'
74 s = 'K';
75 end
76 end