How to handle empty input arguments with the arguments block?

169 vues (au cours des 30 derniers jours)
pedro
pedro le 23 Avr 2023
Commenté : Brian Kardon le 11 Mar 2024
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 commentaire
Paul
Paul le 23 Avr 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?

Connectez-vous pour commenter.

Réponses (2)

LeoAiE
LeoAiE le 23 Avr 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 commentaires
pedro
pedro le 23 Avr 2023
Thank you for your reply. If that's the only way, that's the only way.
But that's exactly what I was hoping the arguments block would let me to avoid. Should the order of the arguments change, or another input argument be added (other than at the end), all that code has to be rewritten.
It seems like a missed opportunity to neatly get rid of "if nargin < N" type code.
Brian Kardon
Brian Kardon le 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.

Connectez-vous pour commenter.


Paul
Paul le 23 Avr 2023
Modifié(e) : Paul le 25 Avr 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.
  1 commentaire
pedro
pedro le 25 Avr 2023
Thank you for your reply. Yes, it seems like that's what's needed, just as before. I was just hoping that the arguments block would itself allow some syntax I might be missing, which would take care of that. Apparently not.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by