필터 지우기
필터 지우기

validate arguments stop implicit expansion

조회 수: 26 (최근 30일)
Nilay Modi
Nilay Modi 2024년 7월 13일 15:43
댓글: Nilay Modi 2024년 7월 14일 10:17
classdef Car
properties
id
position
end
methods
function obj = Car(id, position)
arguments
id (1,1) {mustBeNumeric, mustBePositive, mustBeInteger, mustBeNonempty}
position (1,3) double {mustBeVector, mustBeNonempty}
end
obj.id = id;
obj.position = position;
end
end
end
myCar = Car(42, 20)
Instantiation of myCar creates position vector [20, 20, 20]. This is because of implicity expansion. I am trying to stop this by adding a custom validator, but having trouble.

답변 (1개)

Bhanu Prakash
Bhanu Prakash 2024년 7월 13일 17:01
Hi Nilay,
If I understand it correctly, you want to ensure that the 'position' argument passed is a vector and thereby avoid implicit expansion.
For this use case, you can simply use a custom function (validatePositionArgument, in this case) to verify that the argument 'position' is a vector. If it's not, then the code throws an error. Here is the modified code:
classdef Car
properties
id
position
end
methods
function obj = Car(id, position)
validateattributes(id, {'numeric'}, {'scalar', 'positive', 'integer', 'nonempty'}); % validate 'id'
validatePositionArgument(position); % custom function to validate position
obj.id = id;
obj.position = position;
end
end
end
function validatePositionArgument(position)
% Throws an error if the input 'position' is not a vector or if it's
% length is not equal to 3.
if ~isvector(position) || length(position) ~= 3
error('Position must be a 1x3 vector.');
end
end
Instantiation of myCar with the 'position' argument as a vector:
>> myCar = Car(42, [10, 15, 20])
myCar =
Car with properties:
id: 42
position: [10 15 20]
If the 'position' argument is not a vector or if its size is not equal to 3:
>> myCar = Car(42, 20)
Error using Car>validatePositionArgument (line 21)
Position must be a 1x3 vector.
Error in Car (line 9)
validatePositionArgument(position); % custom function to validate position
For more information on the 'validateattributes' function, you can refer to the following documentation:
I hope this helps!
  댓글 수: 1
Nilay Modi
Nilay Modi 2024년 7월 14일 10:17
How can I do this inside the arguments block?
arguments
id (1,1) {mustBeNumeric, mustBePositive, mustBeInteger, mustBeNonempty}
position (1,3) double {mustBeVector, mustBeNonempty, validatePositionArgument(position)}
end
function validatePositionArgument(position)
% Throws an error if the input 'position' is not a vector or if it's
% length is not equal to 3.
if ~isvector(position) || length(position) ~= 3
error('Position must be a 1x3 vector.');
end
end
Here, inside the validatePositionArgument function, the position argument is already converted to [20, 20, 20]. Your solution doesn't use the argments block. Is it not possible to do this with using arguments block? Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by