Wednesday, June 17, 2020

Function in matlab

In this article, I am going to discuss the function in Matlab. The function is the set of instructions in any programming language and it performs a particular function. Matlab has different features and we can use Matlab for a different purpose and we can use different tools like image processing, machine learning, deep learning, audio processing, etc. Matlab is such a tool where the different tasks can be performed including the programming. In Matlab the syntax of the function is given below:

function [output1,output2,...]=function_name(input_var1,input_var2, ....)
%code is written here
end

For example:
Let's create a function that adds two numbers with the function name add.
 
function x=add(a,b)
x=a+b;
end

The above function is one output variable and two input variables. x is the output variable and a, and b are input variables.
The function add can be called by the following command:
>>add(5,10)
the output will be:
ans= 15
or,
>>var=add(5,10)
output:
var=15

Similarly, let's create another function with two input variables and two output variables with function name fun2. The function can be defined in Matlab as follows:

function [x,y]=fun2(a,b)
x=a+b;
y=a*b;
end

In the above function, a and b are input variables and x and y are the output variables. The x stores the sum of a and b. y stores product of a and b. The function fun2 can be called using the following command:
>>[out1,out2]=fun2(10,5)

the output will be:
out1=15
out2=50

The function in Matlab can be demonstrated in the following video.


For more articles and video please follow this blog and subscribe my youtube channel: