Main Content

mustBeColumn

Validate that value is column vector

Since R2024b

Description

mustBeColumn(value) throws an error if value is not a column vector. A column vector has dimensions of n-by-1. This function does not return a value.

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

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

example

Examples

collapse all

Use mustBeColumn to validate that a value is a column vector.

Create a matrix and test whether it is a column vector. Because the matrix has a nonzero second dimension size, mustBeColumn throws an error.

u = ones(4,5);
mustBeColumn(u)
Value must be a column vector.

Use an arguments block to restrict the function input to a numeric column vector using mustBeColumn and mustBeNumeric.

The DailyTotal function sums the values in an input column vector. If the input is empty ([]), the sum is returned as zero.

function r = DailyTotal(Sales)
    arguments
        Sales {mustBeColumn, mustBeNumeric}
    end
    if isempty(Sales)
        r = 0;
    else
        r = sum(Sales);
    end
end

An alternative way to validate the inputs is with the arguments block

arguments
    Sales (:,1) {mustBeNumeric}
end

However, the use of (:,1) leads to automatic conversion of an input row vector into a column vector. Using mustBeColumn is stricter because it throws an error when the input is not a column vector.

Call the DailyTotal function with a column vector.

w = [35 42 33 70 25 23]';
r = DailyTotal(w)
r =

   228

Input Arguments

collapse all

Value to validate, specified as a column vector.

Extended Capabilities

Version History

Introduced in R2024b