-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_convert_unit.m
53 lines (47 loc) · 1.69 KB
/
pspm_convert_unit.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
function [sts, converted] = pspm_convert_unit(data, from, to)
% ● Description
% pspm_convert_unit is a function to convert between different units
% currently only length units are possible.
% ● Format
% [sts, converted] = pspm_convert_unit(data, from, to)
% ● Arguments
% data: The data which should be converted. Must be a numeric
% array of any shape.
% from: Unit of the input vector.
% Valid units are currently mm, cm, dm, m, km, in, inches
% to: Unit of the output vector.
% Valid units are currently mm, cm, dm, m, km, in, inches
% ● History
% Introduced in PsPM 4.0
% Written in 2018 by Tobias Moser (University of Zurich)
%% Initialise
global settings
if isempty(settings)
pspm_init;
end
sts = -1;
converted = [];
% define conversion settings
converter = struct('length', ...
struct(...
'value', {'mm', 'cm', 'dm', 'm', 'km', 'in', 'inches'}, ...
'factor', {10^-3, 10^-2, 10^-1, 1, 10^3, 2.54e-2, 2.54e-2}...
));
% input checks
% -----------------------------------------------------------------------------
if ~isnumeric(data)
warning('ID:invalid_input', 'Data is not numeric.');
return;
elseif ~(isstr(from) && isstr(to) && all(ismember({from, to}, {converter.length.value})))
valid_units_str = join({converter.length.value}, ', ');
valid_units_str = valid_units_str{1};
warning('ID:invalid_input', 'Both units must be string and must be one of %s.\n', valid_units_str);
return;
end
[~, from_idx] = ismember(from, {converter.length.value});
[~, to_idx] = ismember(to, {converter.length.value});
from_fact = converter.length(from_idx).factor;
to_fact = converter.length(to_idx).factor;
converted = data*from_fact/to_fact;
sts = 1;
return