MATLAB Optional Function Arguments
Ältere Kommentare anzeigen
I am reformatting some functions in my DTFTLab toolbox to use arguments blocks for argument validation.
I was previously using "nargin" and "isempty" statements to allow optional inputs and assign default values. I liked the fact that I could ignore an input by using "[]" and the function would assign it the specified default value.
Using the arguments block is more robust, but I'm unable to find a way to make multiple inputs independently optional.
For example:
function H_filter = bestLowPassFilter(signal, thresholdGain, omega, transition, method, cutoff)
arguments
signal (1,:) double
thresholdGain (1,1) double = 0.1
omega (1,:) double = linspace(-pi, pi, length(signal))
transition (1,1) double = pi/10
method char {mustBeMember(method,{'ideal','tukey','gaussian', 'raised-cosine'})} = 'ideal'
cutoff (1,1) double = NaN
end
%**Continues**
end
I would like every argument to be optional, but you cannot call this function while ignoring an input that precedes any used input.
bestLowPassFilter(X, 0.3, [], [], 'ideal');
If I call the function as shown above, I get an error because omega and transition must be scalar values.
The only workaround I've found is assigning NaN (like I have done with the "cutoff" argument) and then reassigning the true default with an isnan() statement, but that feels like the same as using nargin and isempty() statements.
Is there a way to bypass these inputs in the function call when the argument validation is done with arguments blocks?
Thanks for any advice!
4 Kommentare
Stephen23
am 16 Feb. 2025
I have also written functions in the past that used [] to indicate default values: AFAIKT the ARGUMENTS block does not support this, the closest would be utilise named (optional) arguments.
I've been saying for a while that arguments blocks should have more classdef-style Attributes. Then, you could make an attribute to define a certain input as an indicator that a default should be used.
function notRealCode(signal, thresholdGain, omega, transition, method, cutoff)
arguments
signal (1,:) double
thresholdGain (1,1) double = 0.1
end
arguments(forceDefault=[]) % Make '[]' trigger a default assignment
omega (1,:) double = linspace(-pi, pi, length(signal))
transition (1,1) double = pi/10
method char {mustBeMember(method,{'ideal','tukey','gaussian', 'raised-cosine'})} = 'ideal'
cutoff (1,1) double = NaN
end
%**Continues**
end
(EDIT: I have now submitted this as an enhancement request).
Ethan
am 18 Feb. 2025
Matt J
am 18 Feb. 2025
Yes, I like that better. I added it to my enhancement request.
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 16 Feb. 2025
Suppose you have a function call that you intend to be passing in thesholdGain, and skip omega and transition. So you call
bestLowPassFilter(signal, thresholdGain)
Now you would like to be able to skip unused parameters. Suppose you wanted to call
bestLowPassFilter(signal, transition)
Now, both thresholdGain and transition are scalars. How can argument processing possibly tell whether
bestLowPassFilter(signal, 0.2)
is an example of passing in thresholdGain or an example of passing in transition?
For that matter, how is it to be able to tell that the 0.2 is not an omega vector that happens to be only 1 element long?
The only possible way argument processing could potentially distinguish these variations is by using argname() and hoping that the passed parameter is not an expression and that the variable passed in happens to be distinguishable. For example you could match on whether the variable name happened to begin with 'o' or 'O', or happened to begin with 'tr' or 'TR' or 'Tr' or happened to begin with 't' or 'T' not followed by 'r' or 'R'. This is obviously not very robust.
The major alternative is to use name/value pairs.
1 Kommentar
Ethan
am 18 Feb. 2025
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!