Matlab mustBeA for Matlab < R2020b
Matlab mustBeA is one of the most frequently used features we used from Matlab R2020b and newer. mustBeA allows validating a function argument is one of multiple classes, instead of the single class coercion used by Matlab by default.
Simply create a file in the project directory “mustBeA.m”, or under private/mustBeA.m if it’s a Matlab package.
function mustBeA(x, classes)
% for Matlab < R2020b
arguments
x
classes (1,:) string
end
mustBeMember(class(x), classes)
end
Example
Suppose you have myfun.m:
function myfun(A, B)
arguments
A {mustBeA(A, ["string", "char"])}
B (1,1) {mustBeNumeric}
end
...
end
GNU Octave
Although GNU Octave does not support the arguments ... end
syntax, the
mustBe*
functions
are
supported in GNU Octave.