-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkvar.m
56 lines (52 loc) · 1.48 KB
/
checkvar.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
function [ varout ] = checkvar( varin, varargin )
%CHECKVAR check input variable for specific data type
% Example:
% tf = checkvar(1:10,'scalar')
% tf = checkvar(1,'interger',[0,10])
%
% by Niki 2015/7/17
n = length(varargin);
for i = 1:n
if ischar(varargin{i})
switch varargin{i}
case 'logical'
if islogical(varin)
varout = varin;
elseif all(varin==1|varin==0)
varout = logical(varin);
else
error('not logical input')
end
case 'integer'
if isinteger(varin)
varout = varin;
elseif all(~mod(varin,1))
varout = int16(varin);
else
error('not integer/round number input')
end
case 'scalar'
if numel(varin)==1
varout = varin;
else
error('not scalar input')
end
otherwise
showerr(varargin{i})
end
elseif numel(varargin{i})==2&&isnumeric(varargin{i})
range = varargin{i};
if all(varin<=range(2)&varin>=range(1))
varout = varin;
else
error('input not in range: %f to %f', range)
end
else
showerr(varargin{i})
end
end
function showerr(var)
disp(var)
error('not defined input')
end
end