How to handle empty input arguments with the arguments block?
145 views (last 30 days)
Show older comments
Greetings!
Before the arguments block, it was easy to have a function like
function z9=fn(x9,y9)
% function code
and call it thus:
fn([],3)
handling the possibly absent input/s in a variety of ways.
How can this be handled with the arguments block?
I've tried
arguments
x9 (1,1) double = 1;
end
which gives me an error (Invalid argument at position 1. Value must be a scalar)
and
arguments
x9 double = 1;
end
as well as
arguments
x9 {mustBeScalarOrEmpty, mustBeNumeric} = 1;
end
which do not throw an error, but do not assign the default value 1 either.
Any help would be appreciated.
1 Comment
Paul
on 23 Apr 2023
Hi pedro,
Perhaps I'm missing all of the use cases you'd like to handle. Can you post the full body of a very simple function that doesn't use an arguments block but handles all of the required combinations of inputs?
Answers (2)
LeoAiE
on 23 Apr 2023
You can use the nargin function to handle empty input arguments with the arguments block. The nargin function returns the number of function input arguments passed in the call to the currently executing function.
Here's an example of how you can handle empty input arguments with the arguments block:
function z9 = fn(x9, y9)
arguments
x9 double = 1;
y9 double = 1;
end
if nargin < 1 || isempty(x9)
x9 = 1;
end
if nargin < 2 || isempty(y9)
y9 = 3;
end
% function code
z9 = x9 + y9;
end
Now, you can call the function with empty input arguments:
result = fn([], 3);
the function fn checks the number of input arguments passed using the nargin function. If an input argument is empty or not provided, the function assigns a default value to it.
2 Comments
Brian Kardon
on 11 Mar 2024
I agree with pedro - this is a missed opportunity to allow users to write really clean code. MATLAB should allow ~ to be passed in as an argument in a function call, which would cause the arguments block to susbstitute the default value.
Paul
on 23 Apr 2023
Edited: Paul
on 25 Apr 2023
It sees like, at least based on the examples in the Question, the desire is to have a value of empty be overwritten with some other value. But as the Question stated, an empty input allows for "handling the possibly absent input/s in a variety of ways," although it's very important to understand that an input with an empty value is not absent. To allow for those "variety of ways" it seems like
if isempty(x9)
% do something
end
in the executable part of the function is what's needed.
I suppose if overwriting the empty value with some other value is the only action needed, then maybe such a feature could be implemented in the arguments block, but it feels like doing so opens the door to other users' desires for overwriting other special values on input as well.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!