📜  如何在 MATLAB 中查找函数参数的数量?

📅  最后修改于: 2022-05-13 01:55:29.487000             🧑  作者: Mango

如何在 MATLAB 中查找函数参数的数量?

MATLAB 中传递的函数参数的数量将在下一篇文章中确定。与 C、C++ 和Java不同,MATLAB 可以容纳提供给函数的可变数量的参数而不会引发错误。我们将介绍如何确定提供给函数的实际参数数量并进行适当的计算。

C
#include 
int main()
{
int x =  40;
int y = 50;
printf("Sum of the numbers : %d", GFG(x,y));
int GFG(int a1,int a2, int an)
{
return a1 + a2+ an;
}
}


Matlab
% MATLAB Code for addition 
x = 4;
y = 5;
fprintf("Addition of numbers : %d",GFG(x,y));
function sum = GFG(int a1,int a2..........int an)
         sum = a1 + a2 .... + an;
end


Matlab
% MATLAB code for add
% initialize variables
a = 5;  %integer variable a
b = 8;  %integer variable b
c = 15; %integer variable c
  
% here variable a,b,c is passed in 
% the function, so sum should be 'a+b+c' i.e 28
fprintf("Addition of three numbers : %d",add_gfg(a,b,c)); 
  
% here variable a,b is passed in the function, 
% so sum should be 'a+b' i.e 13
fprintf("Addition of two numbers : %d",add_gfg(a,b));
  
% here variable a is passed in the function, 
% so sum should be 'a' i.e 5
fprintf("Addition of one number : %d",add_gfg(a));
  
% function add_gfg returns the sum 
% of the input variables no return function 
% is required in case of matlab we initialize the
% variable in which we want to store the final answer and
% return that final answer to the calling 
% function, here it is sum
  
function sum = add_gfg(a,b,c)
  
    cases  = nargin;  % nargin: returns the actual number
                      % of input arguments 
      
    switch cases 
        case 1   % if number of input arguments passed is 1,
                 % return a
            sum = a;
        case 2     % if number of input arguments passed is 2,
                 % return sum of 2 numbers
            sum = a + b;
        case 3     % if number of input arguments passed is 3,
                 % return sum of 3 numbers
            sum = a + b + c;
     end
end


Matlab
% MATLAB code
function_name = 'gfg'; %name of the function
  
% Following line prints the number of input arguments
% that appear in the function gfg i.e 3
fprintf("\nThe number of arguments appearing in the function gfg
 is %d",nargin(function_name));
   
 % Name of the function
function_name1 = 'gfg1'; 
  
% Following line prints the number of input
% arguments that appear in the function gfg1 i.e 2
fprintf("\nThe number of arguments appearing in the 
function gfg1 is %d",nargin(function_name1));
  
% The function gfg is not called nor does it print anything
% it is important to create the function or it shows error
% when nargin('gfg') is executed.
  
function gfg(input1,input2,input3)
     %void function
end
  
  
% The function gfg1 is not called nor
% does it print anything
% it is important to create the function or it shows 
% error when nargin('gfg1') is executed.
  
function gfg1(input1)
    % void function
end


输出:

too few arguments to function ‘GFG’

MATLAB

% MATLAB Code for addition 
x = 4;
y = 5;
fprintf("Addition of numbers : %d",GFG(x,y));
function sum = GFG(int a1,int a2..........int an)
         sum = a1 + a2 .... + an;
end

输出:

Addition of numbers : 9

纳尔金():

它返回提供给调用当前正在执行的函数的函数输入参数的数量。 nargin() 帮助您确定发送到函数中的实际输入参数的数量,以便您可以根据这些参数执行所需的计算。

假设如果我们要创建一个名为“add_gfg”的函数,则使用以下 MATLAB 代码。

例子:

MATLAB

% MATLAB code for add
% initialize variables
a = 5;  %integer variable a
b = 8;  %integer variable b
c = 15; %integer variable c
  
% here variable a,b,c is passed in 
% the function, so sum should be 'a+b+c' i.e 28
fprintf("Addition of three numbers : %d",add_gfg(a,b,c)); 
  
% here variable a,b is passed in the function, 
% so sum should be 'a+b' i.e 13
fprintf("Addition of two numbers : %d",add_gfg(a,b));
  
% here variable a is passed in the function, 
% so sum should be 'a' i.e 5
fprintf("Addition of one number : %d",add_gfg(a));
  
% function add_gfg returns the sum 
% of the input variables no return function 
% is required in case of matlab we initialize the
% variable in which we want to store the final answer and
% return that final answer to the calling 
% function, here it is sum
  
function sum = add_gfg(a,b,c)
  
    cases  = nargin;  % nargin: returns the actual number
                      % of input arguments 
      
    switch cases 
        case 1   % if number of input arguments passed is 1,
                 % return a
            sum = a;
        case 2     % if number of input arguments passed is 2,
                 % return sum of 2 numbers
            sum = a + b;
        case 3     % if number of input arguments passed is 3,
                 % return sum of 3 numbers
            sum = a + b + c;
     end
end

输出 :

代码说明:上面的例子接受输入参数并返回传入的整数总数。调用函数'add_gfg'接受输入参数,nargin返回输入参数的总数。如果输入参数为三个,则 nargin 返回三个并将它们存储在变量“cases”中。然后使用 switch 语句计算输入参数的总数,该语句保存在变量 'sum 中并返回给调用者函数。

nargin(函数名):

此函数返回出现在函数“function_name”中的输入参数的数量。它返回可以在函数中传递的输入参数的总长度。例如。函数geeks_for_geeks(int a1, int a2,….int an),该函数可以接收总共 n 个参数,因此“nargin(geeks_for_geeks)”将返回“n”,即总共 n 个参数。

例子:

MATLAB

% MATLAB code
function_name = 'gfg'; %name of the function
  
% Following line prints the number of input arguments
% that appear in the function gfg i.e 3
fprintf("\nThe number of arguments appearing in the function gfg
 is %d",nargin(function_name));
   
 % Name of the function
function_name1 = 'gfg1'; 
  
% Following line prints the number of input
% arguments that appear in the function gfg1 i.e 2
fprintf("\nThe number of arguments appearing in the 
function gfg1 is %d",nargin(function_name1));
  
% The function gfg is not called nor does it print anything
% it is important to create the function or it shows error
% when nargin('gfg') is executed.
  
function gfg(input1,input2,input3)
     %void function
end
  
  
% The function gfg1 is not called nor
% does it print anything
% it is important to create the function or it shows 
% error when nargin('gfg1') is executed.
  
function gfg1(input1)
    % void function
end

输出 :

代码说明:在下面的例子中,我们创建了两个函数'gfg'和'gfg1',它们分别接受输入参数(input1,input2,input3)和(input1)。因此,当我们调用命令 nargin('gfg') 和 nargin('gfg1') 时,它返回 3 和 1,因为它们中出现的参数数量分别为 3 和 1。