필터 지우기
필터 지우기

How to handle empty input arguments with the arguments block?

조회 수: 109 (최근 30일)
pedro
pedro 2023년 4월 23일
댓글: Brian Kardon 2024년 3월 11일
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
Paul
Paul 2023년 4월 23일
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

LeoAiE
LeoAiE 2023년 4월 23일
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
pedro
pedro 2023년 4월 23일
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 2024년 3월 11일
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
Paul 2023년 4월 23일
편집: Paul 2023년 4월 25일
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
pedro
pedro 2023년 4월 25일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by