MATLAB Optional Function Arguments
81 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Matt J
2025 年 2 月 16 日 21:42
編集済み: Matt J
2025 年 2 月 16 日 22:02
Using the arguments block is more robust, but I'm unable to find a way to make multiple inputs independently optional.
The way you are meant to do that in the framework of arguments...end block processing is to convert those arguments to non-positional ones.
X=rand(1,10);
bestLowPassFilter(X, 0.3, method ='ideal')
function bestLowPassFilter(signal, thresholdGain, options)
arguments
signal (1,:) double
thresholdGain (1,1) double = 0.1
options.omega (1,:) double = linspace(-pi, pi, length(signal))
options.transition (1,1) double = pi/10
options.method char {mustBeMember(options.method,{'ideal','tukey','gaussian', 'raised-cosine'})} = 'ideal'
options.cutoff (1,1) double = NaN
end
disp 'Arguments processed'
end
その他の回答 (2 件)
Walter Roberson
2025 年 2 月 16 日 21:40
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.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!