Open In App

Function Argument Validation in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

The basics of Function Argument is, ” The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code.”

Function Argument:

The communication between values and defined called function, using calling argument or parameters that are called function argument.  And the technique or the way of declaring the specific restrictions on function parameters using a validation process is called Function Argument Validation.  

In Programming, there are two types of argument considered –

  1.  Actual Argument – It’s a kind of called function which is used on calling time, like whatever argument we are passing that is called an Actual Argument.
  2. Formal Argument – A defined function that contains some operation is called a Formal Argument. 

Block Syntax for Function Validation

Function defines the arguments or parameters in a particular block of code, which is known as syntax. So here below we take an example of function block syntax, and it’s helping to reduce the function errors.  

function validateFunction( inputArgument) 

               arguments

                    inputArgument

                       %% Specify the size, className,Function and default values.

               end

        % Function code

end      

So as you can see here we have taken a formal argument (parameter) function. the function name is a validated function that will pass the same input parameters according to the requirement. After that, we have to declare the statement inside the arguments like whatever we want to execute through functions like the size, class name function, and default values.

The function argument validation has some criteria when we write our code in the editor so we have to make sure that the argument declaration can include the same kinds of restrictions shown below.

  1. Size – The length of each dimension should be enclosed in parentheses ( ), it specified nonnegative integer numbers or colons (:).
  2. Class – In MATLAB when we define the class so it should be in a word. it can be a char, string, or user-defined class. 
  3. Function – Inside the argument, we can use a number of functions that should be enclosed in curly brackets { fnc1, fnc2, ……, fancy}.

So, let’s start with how to find the Number of function arguments you have to just use the MATLAB tool to run the following programs.

We have two types of Number argument Validation

nargin in Argument Validation:

nargin means a number of argument inputs, using (nargin) switch case we execute a statement based on the number of inputs that have been provided.

Example 1:

Matlab




%% Find the number of function argument
% using Number of argument 
% inputs MATLAB program
function c = addme(a,b)
switch nargin
case 2
c = a + b;
case 1
c= a + a;
otherwise
c = 0;
End


Output:

>> addme(42)
    ans = 84
    
 >> addme(2,4000)
    ans = 4002
    
 >> addme
    ans = 0    

nargout in Argument Validation:

nargout means the Number of arguments output, that identified the number of output that has been requested by the user.

Example 2:

Matlab




% Find the number of function argument 
% using number of argument 
% output MATLAB program
function [result,absResult] = addme2 (a, b)
switch nargin
case
result = a + b;
case
result = a + a;
otherwise 
result = 0;
end 
if nargout > 1
absResult = abs(result);
End


Output:

 >> value = addme2(11, -22)
       value = -11
 >> [value, absValue] = addme2(11, -22)
       value = -11
       absValue = 11

Validate the Number of Function Argument 

We can also have a custom function check whether it receives a valid number of inputs or outputs. MATLAB does perform some argument checks automatically for the others we have check (nargin) check and (nargout) check. in cases where a number of inputs are defined in the functions definition MATLAB check whether the function has received more number of argument than expected. 

Example 3:

Matlab




% MATLAB Code
function [x , y] = myFunction( a,b,c)
[x , y] = myFunction(1,2,3,4)


Output:

Error using myFunction
Too many input arguments.

In the above example definition of myFunction is clearly defined that it will take only three input arguments are expected. so if we pass more than three arguments MATLAB throughs an error, so we have to ensure that how many arguments we are passing through function definition. so in this condition MATLAB check automatically, there is no need to use nargin and nargout check.

Example of Function Argument Validation

Here we take an example of the Define Repeating Positional Input Argument, this kind of argument that can be repeated zero or more times to a function call. so, in this program RepeatFunction function accepts repeating arguments a, b, and style.  in the function a and b to vector of double values, and Restricted style to the string “–” and “:”.

Write a program in MATLAB editor-

Matlab




% MATLAB Code
function RepeatFunction(a,b,style)
arguments(Repeating)
a(1,:)double
b(1,:)double
style{mustBeMember(style,["--",":"]}
end
x = reshape([a;b;style],1,[]);
if ~isempty(x)
plot(x{:});
   end
end


Output:

 

After writing all the required input into Command Window again we hit Enter then we will get the figure output of the Repeating function argument program.

 

 



Last Updated : 14 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads