Main Content

mustBeVector

Validate that value is vector

    Description

    example

    mustBeVector(value) throws an error if value is not a vector. A vector has dimension of 1-by-n or n-by-1. This function does not return a value.

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

    mustBeVector calls the following function to determine if the input is a vector:

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

    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.

    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 the function input to a numeric vector using mustBeVector and mustBeNumeric. Allow an empty value using the mustBeVector allow-all-empties option.

    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 {mustBeVector(DailyTotals,'allow-all-empties'), mustBeNumeric}
        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 or column vector.

    Tips

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

    Introduced in R2020b