keep live-function (member?) help from repeating options?

1 次查看(过去 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

回答(1 个)

Rishav
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.

类别

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