keep live-function (member?) help from repeating options?
조회 수: 3 (최근 30일)
이전 댓글 표시
Is there a way to avoid repeated help/doc options?
I see help/doc syntax like this for a live function member (in its own file):
x = myFun(this, options, options, options)
versus syntax like this for an ordinary function member (ditto):
x = myFun(this, options)
for an arguments block like this:
arguments
this { mustBeNonmissing }
options.A (1,1) double = 1.0;
options.B double = 0;
options.C (1,1) boolean = false;
end
댓글 수: 0
답변 (1개)
Rishav
2023년 9월 5일
편집: Rishav
2023년 9월 5일
Hi T. David,
Instead of using 'options', you can use 'inputParser' class in MATLAB to handle the input arguments in a more flexible manner.
Here's an example of how you can modify your code:
function x = func(this,varargin)
% Create a parser object
parser = inputParser;
% Define the compulsory parameter 'this'
addRequired(parser,'this',@mustBeNonmissing);
% Define the input parameters
addParameter(parser, 'A', 1.0, @isnumeric);
addParameter(parser, 'B', 0, @isnumeric);
addParameter(parser, 'C', false, @islogical);
% Parse the input arguments
parse(parser, this, varargin{:});
% Access the values
thisValue = parser.Results.this;
optionA = parser.Results.A;
optionB = parser.Results.B;
optionC = parser.Results.C;
end
By using the 'inputParser' class, you can define compulsory parameters (e.g., 'this') and optional parameters ('A', 'B', 'C') with their default values and validation functions.
'varargin' allows you to pass multiple parameters to the function, making it more flexible and avoiding the use of 'options' argument.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!