Matlab

How to Create a Function in MATLAB?

Functions in MATLAB help to reuse code. They allow us to encapsulate a set of instructions into a single unit, which can then be called from anywhere in code. MATLAB functions make code reusable and easy to understand. This article covers how we can define a new function and call it inside the MATLAB code.

Creating a Function

Functions in MATLAB are defined using the function keyword. Following syntax is followed for defining a new function in MATLAB:

function [output1, output2, ...] = functionName(input1, input2, ...)

The function keyword tells MATLAB that you are defining a function. Here the defined function has the name functionName. The input1, input2, … are the input arguments to the function. The output1, output2, … are the output arguments from the function.

The body of the function is a block of MATLAB code that will be executed when the function is called.

Below code calculates square of a number using MATLAB function:

values = 8;

squared_values = square_numbers(values);

disp(squared_values);

function squared_values = square_numbers(values)

squared_values = values .^ 2;

end

The code starts by assigning the value 8 to the variable values. The next line calls the function square_numbers with the argument values. The function expects a single input argument, which is the variable values in this case.

The function square_numbers takes the input value and calculates the square of the defined value using the exponentiation operator (.^). It assigns the squared values to the variable squared_values.

At the end code uses the disp function to display the contents of the variable squared_values. This line prints the squared values to the output console.

Calling a Function

To use a function, you need to mention the name of the function and provide the required information or values inside parentheses. For example, to call the square_numbers function, you would use the following code:

squared_values = square_numbers(values);

Function with Multiple Outputs

Here’s an example of a MATLAB function that takes two input values and returns the sum, difference, and product as multiple outputs:

a = 5;

b = 3;

[sum_result, diff_result, prod_result] = calculate_operations(a, b);

function [sum_result, diff_result, prod_result] = calculate_operations(a, b)

sum_result = a + b;

diff_result = a - b;

prod_result = a * b;

 

fprintf('Sum: %d\n', sum_result);

fprintf('Difference: %d\n', diff_result);

fprintf('Product: %d\n', prod_result);

end

In this example, the function is called calculate_operations that takes two input arguments, that are a and b. Inside the function, it performs mathematical operations on a and b. The results are stored in the variables sum_result, diff_result, and prod_result, respectively.

To use this function, you can call it with two input values and receive the results as multiple output arguments.

After executing this code, the variables sum_result, diff_result, and prod_result will contain the sum, difference, and product of a and b, respectively. You can then use these results for further calculations or display them as desired.

Creating Multiple Functions in MATLAB

Here’s an example of multiple MATLAB functions for performing addition, subtraction, and multiplication operations to illustrate how to create multiple functions in MATLAB:

a = 5;

b = 3;

sum_result = addition(a, b);

diff_result = subtraction(a, b);

prod_result = multiplication(a, b);

function sum_result = addition(a, b)

sum_result = a + b;

fprintf('Sum: %d\n', sum_result);

end

function diff_result = subtraction(a, b)

diff_result = a - b;

fprintf('Difference: %d\n', diff_result);

end

function prod_result = multiplication(a, b)

prod_result = a * b;

fprintf('Product: %d\n', prod_result);

end

In this example, three separate functions are defined: addition, subtraction, and multiplication. Each function takes two input arguments, a and b, and performs the respective operation. The defined MATLAB function can be used by calling them individually.

After executing this code, the variables sum_result, diff_result, and prod_result will contain the result of the addition, subtraction, and multiplication operations, respectively.

A screenshot of a computer Description automatically generated with medium confidence

Conclusion

Functions in MATLAB help us to reuse code and multiple ways. They allow us to encapsulate a set of instructions into a single unit, which can then be called from anywhere in code. To define the new MATLAB function the function keyword. The function contains the input and output arguments. These arguments help to display the output on the command window. Read more about defining a function in MATLAB in this article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.