1
- function [] = assertPositiveIntegers(input , allowNaN , allowInf , name )
1
+ function [] = assertPositiveIntegers(input , name , allowNaN , allowInf )
2
2
%% Checks that an input consists of positive integers. Optionally allows
3
3
% NaN and Inf values. Returns customized error messages.
4
4
%
5
- % dash.assertPositiveIntegers(input, allowNaN, allowInf, name)
5
+ % dash.assertPositiveIntegers(input, name)
6
+ % Checks the input consists of positive integers. Does not allow NaN or Inf
7
+ %
8
+ % dash.assertPositiveIntegers(input, name, allowNaN, allowInf)
9
+ % Specify whether to allow NaN or Inf.
6
10
%
7
11
% ----- Inputs -----
8
12
%
9
13
% input: The input being checked
10
14
%
11
- % allowNaN: A scalar logical. Whether to allow NaN values in the input .
15
+ % name: The name of the input. A string. Used for custom error messages .
12
16
%
13
- % allowInf: A scalar logical. Whether to allow Inf values in the input.
17
+ % allowNaN: A scalar logical that indicates whether to allow NaN values in
18
+ % the input (true) or not (false -- default).
14
19
%
15
- % name: The name of the input. Used for custom error messages.
20
+ % allowInf: A scalar logical that indicates whether to allow Inf values in
21
+ % the input (true) or not (false -- default).
16
22
17
- % Process NaNs
18
- if allowNaN
19
- input(isnan(input )) = 1 ;
20
- elseif any(isnan(input ),' all' )
21
- error(' %s may not contain NaN.' , name );
23
+ % Defaults
24
+ if ~exist(' allowNaN' ,' var' ) || isempty(allowNaN )
25
+ allowNaN = false ;
26
+ end
27
+ if ~exist(' allowInf' ,' var' ) || isempty(allowInf )
28
+ allowInf = false ;
22
29
end
23
30
24
- % Process Inf
25
- if allowInf
26
- input(isinf(input )) = 1 ;
27
- elseif any(isinf(input ),' all' )
28
- error(' %s may not contain Inf.' , name );
31
+ % Require numeric
32
+ if ~isnumeric(input )
33
+ error(' %s must be numeric' , name );
29
34
end
30
35
31
- % Everything else
32
- if ~isnumeric(input ) || ~isreal(input ) || any(input < 1,' all' ) || any(mod(input ,1 )~=0 ,' all' )
33
- error(' %s can only contain positive integers.' , name );
36
+ % Process NaN and Inf
37
+ dash .assertRealDefined(input , name , allowNaN , allowInf );
38
+ input(isnan(input )) = 1 ;
39
+ input(isinf(input )) = 1 ;
40
+
41
+ % Check for positive integers
42
+ if any(input < 1,' all' ) || any(mod(input ,1 )~=0 ,' all' )
43
+ error(' %s must only contain positive integers.' , name );
34
44
end
35
45
36
46
end
0 commit comments