Main Content

mustBeVector

Validate that value is vector

    Description

    example

    mustBeVector(value)throws an error ifvalueis not a vector. A vector has dimension of 1-by-norn-by-1. This function does not return a value.

    mustBeVector(value,"allow-all-empties")throws an error ifvalueis not a vector or not an empty array.

    mustBeVectorcalls the following function to determine if the input is a vector:

    Class support: All numeric classes,logical, and MATLAB®classes that overloadisvector.

    Examples

    collapse all

    Determine if value 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.

    mustBeVectorthrows 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)

    mustBeVectorexecutes without throwing an error or returning a value.

    Use an arguments block to restrict the function input to a numeric vector usingmustBeVectorandmustBeNumeric. Allow an empty value using themustBeVectorallow-all-emptiesoption.

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

    functionr = WeeklyTotals(DailyTotals)argumentsDailyTotals{mustBeVector(DailyTotals,'allow-all-empties'), mustBeNumeric}endifisempty(DailyTotals) r = 0;elser = sum(DailyTotals);endend

    Passing an empty value to the function is allowed.

    r = WeeklyTotals([])
    r = 0

    Input Arguments

    collapse all

    Value to validate, specified as a row or column vector.

    Tips

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

    Introduced in R2020b