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

1 Ansicht (letzte 30 Tage)
T. David
T. David am 3 Nov. 2022
Bearbeitet: Rishav am 5 Sep. 2023
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

Antworten (1)

Rishav
Rishav am 5 Sep. 2023
Bearbeitet: Rishav am 5 Sep. 2023
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.

Kategorien

Mehr zu Foundation and Custom Domains finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by