nargin of optional arugments

70 ビュー (過去 30 日間)
Hupeng
Hupeng 2025 年 2 月 11 日 9:26
コメント済み: Hupeng 2025 年 2 月 19 日 1:40
function func(arg1,options)
arguments
arg1 = 1
options.arg2 = 2
options.arg3 = 3
end
disp(nargin)
end
%%
func(1,arg2=3,arg3=5)
1
The function "func" has 3 input arguments, why nargin is 1? And how to get the number of all input arguments, including optional ones?
  2 件のコメント
Noé
Noé 2025 年 2 月 11 日 10:27
Hi Wang,
As mentioned in https://mathworks.com/help/releases/R2024a/matlab/matlab_prog/nargin-in-argument-validation.html "The value that nargin returns does not include optional input arguments that are not included in the function call. Also, nargin does not count any name-value arguments."
Here arg2 and arg3 are optional arguments whereas arg1 is positional argument.
If you want to know how many arguments have been passed you can:
"Use nargin to determine if optional positional arguments are passed to the function when called. For example, this function declares three positional arguments and a name-value argument. Here is how the function determines what arguments are passed when it is called.
  • nargin determines if the optional positional argument c is passed to the function with a switch block.
  • isfield determines if the name-value argument for Format is passed to the function."
Catalytic
Catalytic 2025 年 2 月 11 日 14:50
編集済み: Catalytic 2025 年 2 月 11 日 14:50
Also, nargin does not count any name-value arguments
I find this a bit of an unsettling design choice. Why not create a new command with this behaviour rather than change the behaviour of old ones?

サインインしてコメントする。

採用された回答

Catalytic
Catalytic 2025 年 2 月 11 日 14:31
編集済み: Catalytic 2025 年 2 月 11 日 14:43
func(1,arg2=3,arg3=5)
NUM_ARGSIN = 3
options = struct with fields:
arg2: 3 arg3: 5
function func(arg1,names,values)
arguments
arg1 = 1
end
arguments (Repeating)
names string
values double
end
NUM_ARGSIN = numel(names)+1
nvp=[names;values];
options=struct(nvp{:})
end
  2 件のコメント
Matt J
Matt J 2025 年 2 月 11 日 18:02
編集済み: Matt J 2025 年 2 月 11 日 18:03
If you're going to look at it that way, I think it should be,
func()
NUM_ARGSIN = 0
func(10)
NUM_ARGSIN = 1
func(1,arg2=3,arg3=5)
NUM_ARGSIN = 3
function func(arg1,names,values)
arguments
arg1 = 1
end
arguments (Repeating)
names string
values double
end
NUM_ARGSIN = nargin - numel(names)
nvp=[names;values];
options=struct(nvp{:});
end
Hupeng
Hupeng 2025 年 2 月 19 日 1:40
Thank you.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2025 年 2 月 11 日 12:28
編集済み: Matt J 2025 年 2 月 11 日 12:33
The function "func" has 3 input arguments, why nargin is 1?
There are 5 input arguments, not 3:
func(1,arg2=3,arg3=5)
NUM_ARGSIN = 5
arg1 = 1
options = struct with fields:
arg2: 3 arg3: 5
function func(varargin)
NUM_ARGSIN = nargin
[arg1,options]=doValidations(varargin{:});
disp ' ', arg1, options
end
function [arg1,options]=doValidations(arg1,options)
arguments
arg1 = 1
options.arg2 = 2
options.arg3 = 3
end
end

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by