mustBeUnderlyingType
Validate that value has specified underlying type
Description
mustBeUnderlyingType(
throws an error if value
,typenames
)value
does not have any of the underlying data types
specified by typenames
. This function does not return a value.
mustBeUnderlyingType
calls the following function to determine if
the input has the specified underlying type:
Class support: All MATLAB® classes
Examples
Use mustBeUnderlyingType
to validate that the
input has underlying type double
.
Create a distributed array (requires Parallel Computing Toolbox™) and then validate that the underlying data type is
double
.
x = distributed(single(1:10));
mustBeUnderlyingType(x,"double")
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).
Value must have one of these underlying data types: 'double'.
mustBeUnderlyingType
throws an error because the underlying
type of the distributed array is single
.
You can specify more than one data type to check
against. (since R2024a) Define a second distributed array of type
double
.
y = distributed(double(1:10));
mustBeUnderlyingType
does not throw an error for
x
or y
when both double
and
single
are specified.
mustBeUnderlyingType(x,["double","single"]) mustBeUnderlyingType(y,["double","single"])
Use mustBeUnderlyingType
to restrict the input
argument values that are accepted by a function. Include an arguments
block that validates the input arguments.
This function declares one input argument. In the arguments
block, the input is required to have an underlying data type of
single
.
function mbSingle(input) arguments input {mustBeUnderlyingType(input,"single")} end disp("Input is class " + class(input) + ... " with underlying type " + underlyingType(input) + ".") end
Create a distributed
vector (requires Parallel Computing Toolbox) that has underlying data of type single
.
x = distributed(single(1:10));
Because the input passes the argument validation, the mbSingle
function prints information about the class and underlying type.
mbSingle(x)
Input is class distributed with underlying type single.
mustBeUnderlyingType
identifies a variable by
its container class instead of its contents when the data type of the contents does not
change the behavior of the container. Cell arrays and tables are examples of this type of
container.
This function declares one input argument. In the arguments
block, the input is required to have an underlying data type of
double
.
function mbDouble(input) arguments input {mustBeUnderlyingType(input,"double")} end disp("Input is class " + class(input) + ... " with underlying type " + underlyingType(input) + ".") end
Call the function with a cell array that contains only double
values. Because mustBeUnderlyingType
identifies the data type as a
cell array, the validation fails even though the contents are double
.
x = {1,2,3}; mbDouble(x)
Error using mbDouble (line 3) mbDouble(x) ^ Invalid argument at position 1. Value must have one of these underlying types: double.
This function, which requires that the input be a cell array, accepts
x
without error.
function mbCell(input) arguments input {mustBeUnderlyingType(input,"cell")} end disp("Input is class " + class(input) + ... " with underlying type " + underlyingType(input) + ".") end
mbCell(x)
Input is class cell with underlying type cell.
For more examples, see underlyingType
.
Input Arguments
Value to validate, specified as a scalar, array, or object.
Example: mustBeUnderlyingType(magic(4),"single")
Name of one or more data types to test, specified as a string array, character vector, or cell array of character vectors.
Example: mustBeUnderlyingType(X,["double","single"])
throws an
error if X
does not have underlying type double
or
single
.
Tips
mustBeUnderlyingType
is designed to be used for property and function argument validation.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
The mustBeUnderlyingType
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2020bThe typenames
input argument now accepts multiple data types for
comparison, specified as a string array or a cell array of character vectors.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)