
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validate Number of Function Arguments in MATLAB
In MATLAB, the process of ensuring that a function is executed with the correct and expected input and output arguments is termed as validating the number of function arguments. Go through this tutorial to learn how you can validate the number of function arguments in MATLAB.
Validate Number of Function Arguments in MATLAB
In MATLAB, we can define functions that take a specific number of input and output arguments. We need to ensure that the functions use these input and output arguments properly and also detect errors if any. Therefore, MATLAB provides a way of validating the number of arguments to prevent errors in the function execution and improve reliability of the code.
The following are commonly used methods to validate number of function arguments in MATLAB
Using "narginchk" and "nargoutchk" functions
Using "if" statement and "nargin" and "nargout" functions
Using "varargin & narginchk" and "varargout & nargoutchk" functions
Let us now discuss these methods of validating the number of function arguments in MATLAB with the help of examples.
Using "narginchk" and "nargoutchk" Functions
In MATLAB, the "narginchk" and "nargoutchk" functions are used to validate the number of input and output arguments of function respectively.
These functions validate that the provided number of function arguments are correct or not.
Example 1: Using "narginchk" Function
Let us understand the use of the "narginchk" and "nargoutchk" functions with the help of examples.
% 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
Save this function code in current MATLAB workspace. Then, call the function "fun" as follows
Case I If the number of input arguments are less than the specified (< 3)
fun(3, 2);
The output will be
Error using fun Not enough input arguments.
Case II If the number of input arguments are equal to specified (i.e., 3)
fun(3, 2, 1);
The output will be
3 2 1
Case III If the number of input arguments are more than the specified (> 3)
fun(1, 2, 3, 4, 5)
The output will be
Error using fun Too many input arguments.
This is how we can use the "narginchk" function to validate the number of input arguments in MATLAB.
Example 2: Using "nargoutchk" Function
Now take a look at the following example
% 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
Save this function code as a function in current MATLAB workspace. Then, call the "div" function as follows
Case I Calling the "div" function with specified output arguments
[q, r] = div(10, 3)
The output will be
q = 3.3333 r = 1
Case II Calling the "div" function with less output arguments than specified
[q] = div(10, 3)
The output will be
Error using div Not enough output arguments.
Case III Calling the "div" function with more output arguments than specified
[q, r, x] = div(10, 3)
The output will be
Error using div Too many output arguments.
This is how we can validate the number of output arguments of a function using the "nargoutchk" function in MATLAB.
Using "if" Statement and "nargin" and "nargout" Functions
In MATLAB, we can also validate the number of input and output arguments in a function using the "if" statement and "nargin" and "nargout" functions.
This method is known as the error handling method of validating the number of function arguments.
Example 3: Using "if" and "nargin" Function
Let us understand this method with the help of examples.
% 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
Save this MATLAB function in the current workspace and call it with input arguments as follows.
Case I If the number of input arguments are equal to specified (= 3)
myfun(1, 2, 3);
The output will be
myfun has exactly 3 input arguments.
Case II If the number of input arguments are less than the specified (< 3)
myfun(1, 2);
The output will be
myfun has less number of input arguments than 3.
Case III If the number of input arguments are more than the specified (> 3)
myfun(1, 2, 3, 4, 5);
The output will be
Error using myfun Too many input arguments.
This is how we can use the "if" statement and "nargin" function to validate the number of input arguments in MATLAB function.
Example 4: Using "if" and "nargout" Function
Now take a look at the following example
% 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
Save this MATLAB function the current workspace. Then, call the "funout" function with different number of output arguments as follows
Case I If the number of output arguments are equal to specified (= 2)
[x, y] = funout(5, 3, 7);
The output will be
myfun has exactly 2 output arguments.
Case II If the number of output arguments are less than the specified (< 2)
[x] = funout(5, 3, 7);
The output will be
myfun has less number of output arguments than 2.
Case III If the number of output arguments are more than the specified (> 2)
[x, y, z] = funout(5, 3, 7);
The output will be
Error using funout Too many output arguments.
This example demonstrates how we can use the "if" statement and "nargout" function to validate the number of output arguments in MATLAB.
Using "varargin & narginchk" and "varargout & nargoutchk" Functions
In MATLAB, the "varargin" and "varargout" functions are used to handle the number of input and output arguments of a function dynamically.
We can use the "varargin" and "varargout" functions along with "narginchk" and "nargoutchk" respectively to validate the number of input and output arguments of a function.
Example 5: Using "varargin" and "narginchk" Functions
This method of validating the function arguments in MATLAB is explained with the following example.
% 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
Save this function code in current workspace in MATLAB. Then, call the "fun2" function with different input arguments as follows.
Case I If the number of input arguments are equal to number of specified (3 < input arg < 5)
fun(2, 3, 4);
The output will be
2 3 4
Case II If the number of input arguments are less than specified minimum arguments (< 3)
fun(2, 3);
The output will be
Error using fun2 Not enough input arguments.
Case III If the number of input arguments are more than specified maximum arguments (> 5)
fun2(1, 2, 3, 4, 5, 6);
The output will be
Error using fun2 Too many input arguments.
Example 6: Using "varargout" and "nargoutchk" Functions
This example demonstrates the use of "varargin" and "narginchk" functions to validate the input arguments of a function in 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
Save this function code in the current MATLAB workspace and call the "funx" function to see the output.
Case I If the number of output arguments are equal to the number of specified output arguments (2 < input arg < 3)
[x, y] = funx(5, 3)
The output will be
x = 8 y = 15
Case II If the number of output arguments are less than the specified output arguments (< 2)
[x] = funx(5, 3)
The output will be
Error using funx Not enough output arguments.
Case III If the number of output arguments are more than the specified output arguments (> 3)
[x, y, z, t] = funx(5, 3)
The output will be
Error using funx Too many output arguments.
This example shows that we can use the "varargout" and "nargout" functions to validate the output arguments of a function in MATLAB.
Conclusion
This is all about validating number of function arguments in MATLAB. In this tutorial, I explained all the commonly used methods to validate number of function arguments in MATLAB. In conclusion, validating the number function arguments is an important step to handle the errors correctly and enhance the code reliability.