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