Main Content

mustBeScalarOrEmpty

R2026b

Validate that value is scalar or empty

    Description

    mustBeScalarOrEmpty(value) throws an error if value is not a scalar or is not an empty array. This function does not return a value. mustBeScalarOrEmpty calls the isscalar and isempty functions to determine if the input is a scalar or an empty array.

    Class support: All numeric classes, logical, char, string, and MATLAB® classes that implement isscalar and isempty.

    example

    Examples

    collapse all

    Use mustBeScalarOrEmpty to restrict the values allowed for a function input argument.

    function v = buildVector(inc)
        arguments
            inc {mustBeScalarOrEmpty}
        end
        v = 1:inc:10;
    end

    Passing a nonscalar to the buildVector function results in an error.

    v = buildVector([1 2]);
    Error using buildVector (line 3)
     v = buildVector([1 2]);
                     ^^^^^
    Invalid argument at position 1. Value must be scalar or empty.

    Input Arguments

    collapse all

    Value to validate, specified as a scalar or an empty array. Values that are not a scalar or are not an empty array result in an error.

    Example: []

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | categorical | datetime | duration | calendarDuration
    Complex Number Support: Yes

    Tips

    • mustBeScalarOrEmpty is designed to be used for property and function argument validation.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2020b