Contenido principal

mustBeVector

R2026b

Validate that value is vector

    Description

    mustBeVector(value) throws an error if value is not a vector. A vector has dimensions of 1-by-n or n-by-1. This function does not return a value. mustBeVector calls the isvector function to determine if the input is a vector.

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

    example

    Examples

    collapse all

    Use mustBeVector to validate that a is a row or column vector.

    a = rand(2);
    mustBeVector(a)
    Value must be a 1-by-n vector or an n-by-1 vector.

    mustBeVector throws an error because the input is a 2-by-2 array.

    Reshape the value a to a row vector.

    b = reshape(a,[1,numel(a)])
    mustBeVector(b)

    mustBeVector executes without throwing an error or returning a value.

    Use an arguments block to restrict a function input to a numeric vector using mustBeVector and mustBeNumeric.

    The WeeklyTotals function sums the elements of the input vector.

    function r = WeeklyTotals(DailyTotals)
        arguments
            DailyTotals {mustBeVector,mustBeNumeric}
        end
        r = sum(DailyTotals);
    end

    Passing a row or column vector returns the sum of the vector elements.

    r = WeeklyTotals(1:5)
    r = 
        15

    Input Arguments

    collapse all

    Value to validate, specified as a row or column vector. If you specify a value that is not a vector, 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

    • mustBeVector 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

    expand all