mustBeUnderlyingType
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
Validate That Argument Has Specified Underlying Type
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 underlying type '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"])
Restrict Function Argument Values
Use mustBeUnderlyingType
to restrict the input
argument values that are accepted by a function. You can accomplish this by adding an
arguments
block to the function 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 y = mbSingle(input) arguments input {mustBeUnderlyingType(input,"single")} end disp("Input is class " + class(input) + ... " with underlying type " + underlyingType(input) + ".") end
Call the function with a distributed
vector (requires Parallel Computing Toolbox) that has underlying data of type single
. Since the
input passes the argument validation, the mbSingle
function prints
information about the class and underlying type.
x = distributed(single(1:10)); mbSingle(x)
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6). Input is class distributed with underlying type single.
Input Arguments
value
— Value to validate
scalar | array | object
Value to validate, specified as a scalar, array, or object.
Example: mustBeUnderlyingType(magic(4),"single")
typenames
— Name of one or more data types to test
string array | character vector | cell array of character vectors
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 is X
does not have underlying type double
or
single
.
Data Types: char
| string
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™.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using 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 R2020bR2024a: mustBeUnderlyingType
now accepts more than one data type for comparison
The typenames
input argument now accepts multiple data types for
comparison, specified as a string array or a cell array of character vectors.
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)