如何在MATLAB中驗證函式引數的數量?


在MATLAB中,確保函式以正確且預期的輸入和輸出引數執行的過程稱為驗證函式引數的數量。閱讀本教程,瞭解如何在MATLAB中驗證函式引數的數量。

驗證MATLAB中函式引數的數量

在MATLAB中,我們可以定義接受特定數量的輸入和輸出引數的函式。我們需要確保這些函式正確使用這些輸入和輸出引數,並在出現任何錯誤時檢測到它們。因此,MATLAB提供了一種驗證引數數量的方法,以防止函式執行中的錯誤並提高程式碼的可靠性。

以下是MATLAB中驗證函式引數數量的常用方法:

  • 使用“narginchk”和“nargoutchk”函式

  • 使用“if”語句和“nargin”和“nargout”函式

  • 使用“varargin & narginchk”和“varargout & nargoutchk”函式

現在讓我們透過示例討論這些在MATLAB中驗證函式引數數量的方法。

使用“narginchk”和“nargoutchk”函式

在MATLAB中,“narginchk”和“nargoutchk”函式分別用於驗證函式的輸入和輸出引數的數量。

這些函式驗證提供的函式引數數量是否正確。

示例1:使用“narginchk”函式

讓我們透過示例瞭解“narginchk”和“nargoutchk”函式的用法。

% MATLAB code to validate input arguments using "narginchk" function
% Define a function
function fun(a, b, c)

% Specify number of input arguments
min_input = 3;
max_input = 3;

% Use narginchk function to validate arguments
narginchk(min_input, max_input);

% Display the input arguments
disp([a, b, c]);
end

將此函式程式碼儲存到當前MATLAB工作區。然後,如下呼叫函式“fun”:

情況一 - 如果輸入引數的數量少於指定的 (< 3) -

fun(3, 2);

輸出將是:

Error using fun
Not enough input arguments.

情況二 - 如果輸入引數的數量等於指定的 (即,3) -

fun(3, 2, 1);

輸出將是:

3     2     1

情況三 - 如果輸入引數的數量多於指定的 (> 3) -

fun(1, 2, 3, 4, 5)

輸出將是:

Error using fun
Too many input arguments.

這就是我們如何使用“narginchk”函式來驗證MATLAB中輸入引數的數量。

示例2:使用“nargoutchk”函式

現在來看下面的例子:

% MATLAB code to validate output arguments using "nargoutchk" function
   % Define a function
   function [q, r]= div(a, b)

   % Specify number of output arguments
   min_output = 2;
   max_output = 2;

   % Use "nargoutchk" function to validate output arguments
   nargoutchk(min_output, max_output);

   % Perform division operation
   q = a / b;
   r = rem(a, b);
end

將此函式程式碼儲存為當前MATLAB工作區中的函式。然後,如下呼叫“div”函式:

情況一 - 使用指定的輸出引數呼叫“div”函式 -

[q, r] = div(10, 3)

輸出將是:

q =
   3.3333

r =
   1

情況二 - 使用少於指定的輸出引數呼叫“div”函式 -

[q] = div(10, 3)

輸出將是:

Error using div
Not enough output arguments.

情況三 - 使用多於指定的輸出引數呼叫“div”函式 -

[q, r, x] = div(10, 3)

輸出將是:

Error using div
Too many output arguments.

這就是我們如何在MATLAB中使用“nargoutchk”函式驗證函式的輸出引數的數量。

使用“if”語句和“nargin”和“nargout”函式

在MATLAB中,我們還可以使用“if”語句和“nargin”和“nargout”函式來驗證函式的輸入和輸出引數的數量。

此方法稱為驗證函式引數數量的錯誤處理方法。

示例3:使用“if”和“nargin”函式

讓我們透過示例瞭解此方法。

% MATLAB code to valid input arguments using if and nargin functions
% Create a function
function myfun(a, b, c)

% Validate the input arguments
if nargin == 3
disp('myfun has exactly 3 input arguments.');
else
disp('myfun has less number of input arguments than 3.');
end

將此MATLAB函式儲存在當前工作區中,並使用以下輸入引數呼叫它。

情況一 - 如果輸入引數的數量等於指定的 (= 3) -

myfun(1, 2, 3);

輸出將是:

myfun has exactly 3 input arguments.

情況二 - 如果輸入引數的數量少於指定的 (< 3) -

myfun(1, 2);

輸出將是:

myfun has less number of input arguments than 3.

情況三 - 如果輸入引數的數量多於指定的 (> 3) -

myfun(1, 2, 3, 4, 5);

輸出將是:

Error using myfun
Too many input arguments.

這就是我們如何使用“if”語句和“nargin”函式來驗證MATLAB函式中輸入引數的數量。

示例4:使用“if”和“nargout”函式

現在來看下面的例子:

% MATLAB code to validate output arguments using if and nargout function
   % Create a function
   function [x, y] = funout(a, b, c)

   % Perform output calculations
   x = a + b;
   y = b * c;

   % Validate the output arguments
   if nargout == 2
      disp('myfun has exactly 2 output arguments.');
   else
      disp('myfun has less number of output arguments than 2.');
   end
end

將此MATLAB函式儲存到當前工作區。然後,使用不同數量的輸出引數呼叫“funout”函式:

情況一 - 如果輸出引數的數量等於指定的 (= 2) -

[x, y] = funout(5, 3, 7);

輸出將是:

myfun has exactly 2 output arguments.

情況二 - 如果輸出引數的數量少於指定的 (< 2) -

[x] = funout(5, 3, 7);

輸出將是:

myfun has less number of output arguments than 2.

情況三 - 如果輸出引數的數量多於指定的 (> 2) -

[x, y, z] = funout(5, 3, 7);

輸出將是:

Error using funout
Too many output arguments.

此示例演示瞭如何使用“if”語句和“nargout”函式來驗證MATLAB中的輸出引數的數量。

使用“varargin & narginchk”和“varargout & nargoutchk”函式

在MATLAB中,“varargin”和“varargout”函式用於動態處理函式的輸入和輸出引數的數量。

我們可以分別將“varargin”和“varargout”函式與“narginchk”和“nargoutchk”函式一起使用來驗證函式的輸入和輸出引數的數量。

示例5:使用“varargin”和“narginchk”函式

以下示例說明了這種在MATLAB中驗證函式引數的方法。

% MATLAB code to validate input arguments
function fun2(varargin)

   % Specify the min and max input arguments
   min_input = 3;
   max_input = 5;

   % Validate the number of input arguments
   narginchk(min_input, max_input);
end

將此函式程式碼儲存在MATLAB的當前工作區中。然後,使用不同的輸入引數呼叫“fun2”函式。

情況一 - 如果輸入引數的數量等於指定的數量 (3 < 輸入引數 < 5) -

fun(2, 3, 4);

輸出將是:

2     3     4

情況二 - 如果輸入引數的數量少於指定的最小引數 (< 3) -

fun(2, 3);

輸出將是:

Error using fun2
Not enough input arguments.

情況三 - 如果輸入引數的數量多於指定的最大引數 (> 5) -

fun2(1, 2, 3, 4, 5, 6);

輸出將是:

Error using fun2
Too many input arguments.

示例6:使用“varargout”和“nargoutchk”函式

此示例演示了使用“varargin”和“narginchk”函式來驗證MATLAB中函式的輸入引數。

% MATLAB code to validate output arguments
function varargout = funx(a, b)

   % Perform some calculations on input arguments
   x = a + b;
   y = a * b;

   % Specify the min and max output arguments
   min_output = 2;
   max_output = 3;

   % Validate the number of output arguments
   nargoutchk(min_output, max_output);

   % Assign output values to output arguments
   varargout{1} = x;
   varargout{2} = y;
end

將此函式程式碼儲存在當前MATLAB工作區中,並呼叫“funx”函式以檢視輸出。

情況一 - 如果輸出引數的數量等於指定的輸出引數的數量 (2 < 輸入引數 < 3) -

[x, y] = funx(5, 3)

輸出將是:

x =
   8

y =
   15

情況二 - 如果輸出引數的數量少於指定的輸出引數 (< 2) -

[x] = funx(5, 3)

輸出將是:

Error using funx
Not enough output arguments.

情況三 - 如果輸出引數的數量多於指定的輸出引數 (> 3) -

[x, y, z, t] = funx(5, 3)

輸出將是:

Error using funx
Too many output arguments.

此示例表明我們可以使用“varargout”和“nargout”函式來驗證MATLAB中函式的輸出引數。

結論

這就是關於在MATLAB中驗證函式引數數量的所有內容。在本教程中,我解釋了在MATLAB中驗證函式引數數量的所有常用方法。總而言之,驗證函式引數的數量是正確處理錯誤和提高程式碼可靠性的重要步驟。

更新於:2023年10月25日

瀏覽量:75

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告