-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitconverter.m
83 lines (72 loc) · 1.88 KB
/
unitconverter.m
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
74
75
76
77
78
79
80
81
82
83
%Converts from one unit type to another. Unit types:
%meters = m, feet = ft,
%Celcius = C, Fahrenheit = F, Kelvin = K, Rankin = R,
%Pascals = Pa, pounds per square inch = psi,
%pounds(force only) = lb, newtons = N
function newNumber = unitconverter(number,oldUnit,newUnit)
%%distance
if strcmp(oldUnit,'m') && strcmp(newUnit,'ft')
newNumber = number./0.3048;
end
if strcmp(oldUnit,'ft') && newUnit == 'm'
newNumber = number.*0.3048;
end
%% temperature
if oldUnit == 'F'
if newUnit == 'C'
newNumber = (number-32).*5/9;
elseif newUnit == 'K'
newNumber = (number-32).*5/9+273.15;
elseif newUnit == 'R'
newNumber = number + 459.67;
end
end
if oldUnit == 'C'
if newUnit == 'F'
newNumber = (number.*9/5)+32;
elseif newUnit == 'K'
newNumber = number+273.15;
elseif newUnit == 'R'
newNumber = (number.*9/5)+32 + 459.67;
end
end
if oldUnit == 'K'
if newUnit == 'F'
newNumber = ((number-273.15).*9/5)+32;
elseif newUnit == 'C'
newNumber = number-273.15;
elseif newUnit == 'R'
newNumber = (number.*9/5);
end
end
if oldUnit == 'R'
if newUnit == 'F'
newNumber = number + 459.67;
elseif newUnit == 'C'
newNumber = (number.*5/9)+273.15;
elseif newUnit == 'K'
newNumber = (number.*5/9);
end
end
%% Pressure
if strcmp(oldUnit,'Pa') && strcmp(newUnit,'psi')
newNumber = number/6894.75728;
end
if strcmp(oldUnit,'psi') && strcmp(newUnit,'Pa')
newNumber = number*6894.75728;
end
%% Force
if strcmp(oldUnit,'lb') && newUnit == 'N'
newNumber = number.*4.4482216;
end
if strcmp(oldUnit,'N') && strcmp(newUnit,'lb')
newNumber = number./4.4482216;
end
%% Check to see if got converted
if exist('newNumber','var') == 0
newNumber = number;
disp('Invalid or Unsupported Conversion')
elseif exist('newNumber','var') == 1
end
%end of function
end