inputParser with only a value, not a name-value pair
이전 댓글 표시
Is it possible to use inputParser with only a value, not a name-value pair. So that when you send a function a specific argument, the argument is automatically set to a variable, without a variable name needed to be given in the function call?
Instead of
a = findArea(13,'shape','square');
You could just send findArea 'square', like:
a = findArea(13,'square');
And within the function, it would recognize that square can only be a shape and set shape's value to 'square'.
답변 (1개)
Fangjun Jiang
2018년 7월 13일
0 개 추천
Yes. You would make that a required argument, not an optional argument.
function a = findArea(width,Shape,varargin)
....
addRequired(p,'shape',defaultShape,... @(x) any(validatestring(x,expectedShapes)));
댓글 수: 2
Gregory Dachner
2018년 7월 17일
Fangjun Jiang
2018년 7월 18일
should be
addRequired(p,'shape', @(x) any(validatestring(x,expectedShapes)));
카테고리
도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!