-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvarea.m
More file actions
73 lines (71 loc) · 1.65 KB
/
cvarea.m
File metadata and controls
73 lines (71 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function valOut = cvarea(valIn,unitIn,unitOut)
% Quickly convert area units
% Usage: valOut = cvarea(valIn,unitIn,unitOut)
%
% --- Options ---
% -- Metric -
% sqm, squaremeter
% sqcm, squarecentimeter
% sqmm, squaremillimeter
% squm, squaremicrometer, squaremicron
% sqnm, squarenanometer
% sqkm, squarekilometer
% -- Imperial -
% sqin, squareinch
% sqft, squarefoot
% sqyd, squareyard
% sqmi, squaremile
% -- Other -
% acre
% are
% hectare
% barn
arguments
valIn {mustBeNumeric}
unitIn char
unitOut char
end
if strcmpi(unitIn,unitOut)
valOut = valIn;
return;
end
valOut = valIn.*getScaleFactor(unitIn)./getScaleFactor(unitOut);
end
function sc = getScaleFactor(len)
% Base unit = meters.
switch lower(len)
% Metric
case {'sqm','squaremeter'}
sc = 1;
case {'sqcm','squarecentimeter'}
sc = 1e-4;
case {'sqmm','squaremillimeter'}
sc = 1e-6;
case {'squm','squaremicrometer','squaremicron'}
sc = 1e-12;
case {'sqnm','squarenanometer'}
sc = 1e-18;
case {'sqkm','squarekilometer'}
sc = 1e6;
% Imperial
case {'sqin','squareinch'}
sc = 0.00064516;
case {'sqft','squarefoot'}
sc = 0.09290304;
case {'sqyd','squareyard'}
sc = 0.83612736;
case {'sqmi','squaremile'}
sc = 2589988.110336;
% Other
case 'acre'
sc = 4046.8564224;
case 'hectare'
sc = 10000;
case 'are'
sc = 100;
case 'barn'
sc = 1e-28;
otherwise
error('%s is not a supported unit',len)
end
end