Main Content

mustBeVectorOrEmpty

R2026b

Validate that value is vector or empty

Since R2026b

    Description

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

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

    example

    Examples

    collapse all

    Use mustBeVectorOrEmpty to validate that a value is a vector or an empty array.

    Create a 2-by-2 matrix and test whether it is a vector. mustBeVectorOrEmpty throws an error.

    A = ones(2);
    mustBeVectorOrEmpty(A)
    Value must be a 1-by-n vector, an n-by-1 vector, or empty.

    Next, create a 0-by-3 empty array and test whether it is an empty array. In this case, mustBeVectorOrEmpty does not throw an error.

    A = zeros(0,3);
    mustBeVectorOrEmpty(A)

    Use an arguments block to restrict a function input to a vector or an empty array using mustBeVectorOrEmpty.

    The WeeklyTotals function sums the elements of the input vector. If the input is empty ([]), the sum is returned as zero.

    function r = WeeklyTotals(DailyTotals)
        arguments
            DailyTotals {mustBeVectorOrEmpty}
        end
        if isempty(DailyTotals)
            r = 0;
        else
            r = sum(DailyTotals);
        end
    end

    Passing an empty value to the function is allowed.

    r = WeeklyTotals([])
    r = 
        0

    Input Arguments

    collapse all

    Value to validate, specified as a row vector, column vector, or empty array. If you specify a value that is not a vector or an empty array, the function throws an error.

    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

    • mustBeVectorOrEmpty 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 R2026b