Skip to content

Commit 12fc8b1

Browse files
committed
Updated for alpha release 2
1 parent f5585ca commit 12fc8b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3919
-262
lines changed

@dash/assertPositiveIntegers.m

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
1-
function[] = assertPositiveIntegers(input, allowNaN, allowInf, name)
1+
function[] = assertPositiveIntegers(input, name, allowNaN, allowInf)
22
%% Checks that an input consists of positive integers. Optionally allows
33
% NaN and Inf values. Returns customized error messages.
44
%
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.
610
%
711
% ----- Inputs -----
812
%
913
% input: The input being checked
1014
%
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.
1216
%
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).
1419
%
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).
1622

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;
2229
end
2330

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);
2934
end
3035

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);
3444
end
3545

3646
end

@dash/assertRealDefined.m

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function[] = assertRealDefined(input, name, allowNaN, allowInf, allowComplex)
2+
%% Checks that an input is real, not NaN, and not Inf. Optionally allows
3+
% NaN, Inf, or complex. Returns custom error messages.
4+
%
5+
% dash.assertRealDefined(input, name)
6+
% Checks that an input is real, not NaN and not Inf.
7+
%
8+
% dash.assertRealDefined(input, name, allowNaN, allowInf, allowComplex)
9+
% Optionally allow NaN, Inf, or complex.
10+
%
11+
% ----- Inputs -----
12+
%
13+
% input: The input being checked
14+
%
15+
% name: The name of the input. A string. Used for custom error messages.
16+
%
17+
% allowNaN: Scalar logical indicating whether to allow NaN (true) or not
18+
% (false -- default)
19+
%
20+
% allowInf: Scalar logical indicating whether to allow Inf (true) or not
21+
% (false -- default)
22+
%
23+
% allowComplex: Scalar logical indicating whether to allow complex values
24+
% (true) or not (false -- default)
25+
26+
% Defaults
27+
if ~exist('allowNaN','var') || isempty(allowNaN)
28+
allowNaN = false;
29+
end
30+
if ~exist('allowInf','var') || isempty(allowInf)
31+
allowInf = false;
32+
end
33+
if ~exist('allowComplex','var') || isempty(allowComplex)
34+
allowComplex = false;
35+
end
36+
37+
% Check input
38+
if ~allowNaN && any(isnan(input), 'all')
39+
error('%s may not contain NaN', name);
40+
elseif ~allowInf && any(isinf(input), 'all')
41+
error('%s may not contain Inf', name);
42+
elseif ~allowComplex && ~isreal(input)
43+
error('%s may not contain complex (imaginary) values', name);
44+
end
45+
46+
end

@dash/assertScalarLogical.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function[] = assertScalarLogical(input, name)
2+
%% Checks that an input is a scalar logical. Throws a custom error message
3+
% if not.
4+
%
5+
% dash.assertScalarLogical(input, name)
6+
%
7+
% ----- Inputs -----
8+
%
9+
% input: The input being checked
10+
%
11+
% name: The name of the input. A string. Used for error message.
12+
13+
if ~isscalar(input) || ~islogical(input)
14+
error('%s must be a scalar logical.', name);
15+
end
16+
17+
end

@dash/assertStrFlag.m

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
function[] = assertStrFlag( input, name )
1+
function[input] = assertStrFlag( input, name )
22
%% Checks that an input is a string flag. Returns a customized error
3-
% message if not.
3+
% message if not. Optionally returns input as a string data type.
44
%
5-
% dash.assertStrFlags( input, name )
5+
% dash.assertStrFlag( input, name )
6+
%
7+
% input = dash.assertStrFlag(input, name)
68
%
79
% ----- Inputs -----
810
%
911
% input: A variable being checked.
1012
%
11-
% names: The name of the variables to use in error messages. A string.
13+
% name: The name of the variable to use in the error message. A string.
14+
%
15+
% ----- Outputs -----
16+
%
17+
% input: The input as a string data type.
1218

1319
if ~dash.isstrflag(input)
1420
error('%s must be a string scalar or character row vector.',name);
1521
end
16-
22+
input = string(input);
23+
1724
end

@dash/assertStrList.m

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
function[] = assertStrList(input, name)
1+
function[input] = assertStrList(input, name)
2+
%% Checks that an input is a string list. Returns a customized error
3+
% message if not. Optionally returns input as a string data type.
4+
%
5+
% dash.assertStrList(input, name)
6+
%
7+
% input = dash.assertStrList(input, name)
8+
%
9+
% ----- Inputs -----
10+
%
11+
% input: The input being checked
12+
%
13+
% name: The name of a variable being check. A string.
14+
%
15+
% ----- Outputs -----
16+
%
17+
% input: The input as a string data type.
18+
219
if ~dash.isstrlist(input)
320
error('%s must be a string vector or cellstring vector.', name);
421
end
22+
input = string(input);
23+
524
end

@dash/assertVectorTypeN.m

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function[] = assertVectorTypeN( input, type, N, name )
2+
%% Checks that an input is a vector with length N. Optionally also checks
3+
% the vector is a specific data type. Returns a customized error message if not.
4+
%
5+
% dash.assertVectorTypeN(input, [], N, name)
6+
% Checks the input is a vector of length N.
7+
%
8+
% dash.assertVectorTypeN(input, type, [], name)
9+
% Checks the input is a vector of a specific type.
10+
%
11+
% dash.assertVectorTypeN(input, type, N, name)
12+
% Checks both type and length.
13+
%
14+
% ----- Inputs -----
15+
%
16+
% input: The input being checked.
17+
%
18+
% type: The required data type. Use [] to not check the type.
19+
%
20+
% N: The required length of the vector. Use [] to not check the length.
21+
%
22+
% name: The name of the input. Used for custom error message.
23+
24+
if ~isvector(input)
25+
error('%s must be a vector.', name);
26+
elseif ~isempty(N) && numel(input)~=N
27+
error('%s must have %.f elements, but it has %.f elements instead.', name, N, numel(input));
28+
elseif ~isempty(type) && ~isa(input, type)
29+
error('%s must be a %s vector, but it is a %s vector instead.', name, type, class(input));
30+
end
31+
32+
end

@dash/checkFileExists.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
% Throw error if the file doesn't exist
3434
if isempty(path)
3535
if ~exist
36-
error('Could not find file %s. It may be misspelled or not on the active path.', file);
36+
error("DASH:missingFile",'Could not find file %s. It may be misspelled or not on the active path.', file);
3737
end
3838

3939
% Get the path string if off the active path.
@@ -42,4 +42,7 @@
4242
rmpath(fileparts(file));
4343
end
4444

45+
% Use string internally
46+
path = string(path);
47+
4548
end

@dash/checkIndices.m

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function[indices] = checkIndices( indices, name, length, lengthName )
2+
%% Checks that an input is a set of indices. Indices may be a logical
3+
% vector the length of a dimension, or a vector of linear indices. Linear
4+
% indices may not exceed the dimension length. Returns custom error
5+
% messages. Converts logical indices to linear indices.
6+
%
7+
% indices = dash.checkIndices( indices, name, length, lengthName )
8+
%
9+
% ----- Inputs -----
10+
%
11+
% indices: The indices being checked.
12+
%
13+
% name: The name of the indices. Used for custom error messages.
14+
%
15+
% length: The length of the array dimension. This is the maximum value
16+
% for linear indices and the required length of logical indices.
17+
%
18+
% lengthName: The name of the length of the array dimension. A string.
19+
%
20+
% ----- Outputs -----
21+
%
22+
% indices: Linear indices
23+
24+
% Allow empty call
25+
if ~isequal(indices, [])
26+
27+
% Vector
28+
if ~isvector(indices)
29+
error('%s must be a vector.',name);
30+
end
31+
32+
% Logical indices
33+
if islogical(indices)
34+
if numel(indices)~=length
35+
error('%s is a logical vector, but it is not %s (%.f).', name, lengthName, length);
36+
end
37+
indices = find(indices);
38+
39+
% Numeric indices
40+
elseif isnumeric(indices)
41+
dash.assertPositiveIntegers(indices, name);
42+
if max(indices) > length
43+
error('%s has elements larger than %s (%.f).', name, lengthName, length);
44+
end
45+
46+
% Other types are not allowed
47+
else
48+
error('%s must either be logical or numeric.');
49+
end
50+
end
51+
52+
end

@dash/checkStrsInList.m

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function[k] = checkStrsInList(input, list, name, listName )
2+
%% Checks that an input is a set of strings that are all members of a list.
3+
% Throws a custom error message if not. Returns the indices of the strings
4+
% in the list.
5+
%
6+
% k = dash.checkStrsInList(input, list, name, listMessage)
7+
%
8+
% ----- Inputs -----
9+
%
10+
% input: The input being checked
11+
%
12+
% list: A list of allowed strings. A string vector.
13+
%
14+
% name: The name of the input. A string
15+
%
16+
% listName: Name of the list. A string
17+
18+
% Check the input is a string list
19+
dash.assertStrList(input, name);
20+
input = string(input);
21+
22+
% Check all strings are allowed. Get their indices in the list.
23+
[inList, k] = ismember(input, list);
24+
if any(~inList)
25+
bad = find(~inList,1);
26+
27+
% Informative error message
28+
badName = name;
29+
if numel(input)>1
30+
badName = sprintf('Element %.f in %s (%s)', bad, name, input(bad));
31+
end
32+
error('%s is not a %s. Allowed values are %s.', badName, listName, dash.messageList(list));
33+
end
34+
35+
end

@dash/collectField.m

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function[values] = collectField(s, field)
2+
%% Collects the values in a field of a structure vector.
3+
%
4+
% values = dash.collectField(s, field)
5+
%
6+
% ----- Inputs -----
7+
%
8+
% s: The structure vector
9+
%
10+
% field: The name of the field. A string scalar or character row vector.
11+
%
12+
% ----- Outputs -----
13+
%
14+
% values: The values in the field. A cell vector with one element per
15+
% structure in s.
16+
17+
nEls = numel(s);
18+
values = cell(nEls, 1);
19+
[values{:}] = deal(s.(field));
20+
21+
end

0 commit comments

Comments
 (0)