mustBeVector - Validate that value is vector - MATLAB (original) (raw)
Main Content
Validate that value is vector
Since R2020b
Syntax
Description
mustBeVector([value](#mw%5F7555e92f-2d7a-491e-9c65-1c653cc47e4b))
throws an error ifvalue
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](#mw%5F7555e92f-2d7a-491e-9c65-1c653cc47e4b),"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
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 themustBeVector
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.
Input Arguments
Value to validate, specified as a row or column vector.
Tips
mustBeVector
is designed to be used for property and function argument validation.
Extended Capabilities
The mustBeVector
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2020b