arguments block seems wrong

9 views (last 30 days)
Duijnhouwer on 21 Aug 2020
Commented: Walter Roberson on 22 Aug 2020
the arguments block can be used to validate input arguments. Great! Except that it doesn't seem to work as advertised.
According to the documentation (doc arguments)
"The value assigned to the argument in the function call must be compatible with the specified size, or MATLAB throws an error."
However, when calling this function
function a=f(a)
a (1,3) double
with a scalar
>> f(1)
ans =
1 1 1
The function expands the erroneous scalar argument to a 3-element vector instead of casting the error that was promised!
Why would this be? Am I reading it wrong?
  1 Comment
Duijnhouwer on 21 Aug 2020
>> disp(version) (R2020a) Update 4

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 21 Aug 2020
Documented. The size of the input must be "compatible" taking into account implicit expansion.
Walter Roberson
Walter Roberson on 22 Aug 2020
Ah, this part seems to be the key:
MATLAB indexed assignment rules apply to size specifications. For example, a 1-by-1 value is compatible with the size specified as (5,3) because MATLAB applies scalar expansion. Also, MATLAB row-column conversion applies so that a size specified as (1,:) can accept a size of 1-by-n and n-by-1.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by