Matlab is a powerful software that is widely used in scientific research, engineering, and mathematical analysis. One of the main features of Matlab is its ability to define and use custom functions. Creating your own function in Matlab is not only a useful skill but can also help to optimize your Matlab code and make it more organized. In this article, we will walk you through the process of creating a function in Matlab in relaxed English language.

A function is simply a block of code that performs a specific task and can be used again and again. Functions in Matlab are defined using the keyword ‘function’ followed by the function name and input arguments. The syntax for defining a Matlab function is quite simple, but the process can be a bit confusing for beginners. This article aims to provide a step-by-step guide to creating your own functions in Matlab with examples and explanations in relaxed English language.

Section: How to Make a Function in Matlab

1. What is a Matlab Function and Why is it Important?

Matlab functions are a crucial part of any Matlab script or program. A function is a group of related commands that perform a specific task and can be called from anywhere within a script. Functions help to make a large codebase more organized and easier to manage, as they allow for reusable code. They also make debugging easier, since you can isolate specific parts of your code that may be causing errors. Additionally, by breaking down your code into smaller functions, you can improve its readability and make it more modular.

2. Creating a Basic Matlab Function

Creating a basic Matlab function is straightforward. First, open a new script in Matlab and type the following:

function output = functionName(input)

This line defines the function with the name “functionName” and specifies that it takes an input argument called “input” and returns an output called “output”. You can then write the code for your function below this line, making sure to save the output to the variable “output” at the end.

3. Passing Multiple Arguments to a Function

Sometimes, you may need to pass multiple arguments to a function. To do this, simply include them in the function definition, separated by commas:

function output = functionName(input1, input2, input3)

You can then reference these arguments within your code using their respective variable names.

4. Returning Multiple Outputs from a Function

Similarly, you can return multiple outputs from a function by separating them with commas:

function [output1, output2] = functionName(input)

In this case, you would create two variables to hold the output values and assign them using the same syntax as before.

5. Using Anonymous Functions

Anonymous functions in Matlab are small, nameless functions that can be defined on the fly. They can be useful for defining simple functions without cluttering your codebase with extraneous function files. To create an anonymous function, use the “@” symbol followed by the function definition:

f = @(x) x^2;

This creates a function “f” that squares its input. You can then use this function like any other Matlab function.

6. Documenting Your Functions

It’s good practice to document your functions using comments within the function file. This documentation should include information such as what the function does, what inputs it expects, and what outputs it provides. This helps other developers (or your future self!) to understand the function’s purpose and use it correctly.

7. Debugging Functions

Debugging Matlab functions is similar to debugging regular Matlab code. You can use the built-in debugger by setting breakpoints in your code and stepping through it line by line. Additionally, you can use the “disp” function to print messages to the console during execution, which can be useful for tracking down errors.

8. Testing Your Functions

Before using your functions in production code, it’s always a good idea to test them thoroughly. You can do this by using Matlab’s built-in testing framework, which allows you to write test scripts that check the behavior of your function under various conditions. This can help ensure that your function works as expected and catches errors early on.

9. Organizing Your Functions

As your codebase grows, it’s important to organize your functions in a logical way. One common approach is to create separate subfolders within your project directory for different types of functions. You can also create “helper” functions that are used by multiple other functions, and store them in a separate directory.

10. Sharing Your Functions

If you’ve created a useful Matlab function that others could benefit from, consider sharing it with the Matlab community. You can do this by uploading it to the Matlab File Exchange, which is a central repository for Matlab code. By sharing your code, you can help others learn and improve the Matlab ecosystem as a whole.

Basic Syntax of MATLAB Functions

Now that we have a basic understanding of what functions are and why they are important in MATLAB, let’s dive into the syntax of creating them. The following subheadings will guide you through the process of creating a function in MATLAB.

1. Function Keyword:

We start by defining the function keyword, followed by the name of the function. The function name should be concise and meaningful.

2. Input Arguments:

The input arguments are enclosed within a pair of parentheses after the function name. These arguments are separated by commas and specify the values that the function requires to execute. In case the function does not require any inputs, you can leave the parentheses empty.

3. Output Arguments:

In case the function needs to return some value(s), you can define the output arguments in the same manner as input arguments, within a pair of square brackets just before the function body. If there are multiple output arguments, then they will also have to be separated by commas.

4. Function Body:

After defining the input and output arguments, define the main body of the function, which is denoted by statements or commands that perform a specific task or set of operations. You can use conditional statements, loops, and other MATLAB functionalities within the function body.


It is good practice to add comments within your function, describing what the specific commands do, or any other relevant information about the function. Comments are preceded by a % sign.

6. Local Variables:

You can define local variables inside the function, which are only accessible within the function, and not outside of it. These are useful when you want to use temporary variables within the function body.

7. Recursive Functions:

A recursive function is a function that calls itself repeatedly until it reaches a base case which stops the recursion. MATLAB allows creating recursive functions, provided that the recursive calls are not infinite or infinite-loop prone.

8. Anonymous Functions:

Anonymous functions are functions that are not associated with a specific function name, rather they use a variable name to hold the function. These are useful when you want to create a small, one-line function that serves a specific purpose.

9. Function Handles:

Function handles are a way to pass functions as inputs or outputs to other functions. You can use the ‘@’ symbol to create a function handle.

10. Saving and Accessing Functions:

Once you have created a function, you can save it with the same name as the function name and a .m extension in MATLAB’s search path. This makes it possible to access the function from any location in MATLAB without having to specify the directory path every time.

Creating a Function in MATLAB

Once you have understood the basics of MATLAB and how to use it, you can start creating your own functions. Functions are reusable segments of code that can be called upon multiple times within a program. They are essential for modular programming, where you can divide a problem into smaller parts, making it easy to manage and debug.

In this section, we will cover the steps involved in creating a function in MATLAB.

Step 1: Function Declaration

The first step in creating a function is to declare it. The declaration of a function involves specifying the name of the function, the input arguments, and the output arguments.

The syntax for declaring a function in MATLAB is as follows:

function [output1, output2, …, outputN] = functionName(input1, input2, …, inputN)
where `functionName` is the name given to the function, `output1, output2, …, outputN` are the variables that the function returns or outputs, `input1, input2, …, inputN` are the input variables that the function requires.

For example, let’s create a simple function to add two numbers together and return their sum:

function [sum] = myAddition(num1, num2)
sum = num1 + num2;

Here, `myAddition` is the name of the function, `num1` and `num2` are the input arguments, and `sum` is the output argument.

Step 2: Writing the Function Body

The next step is to write the body of the function or the code that performs the desired task. In our example, we want to add two numbers, so the body of our function will contain the addition operation.

function [sum] = myAddition(num1, num2)
sum = num1 + num2;

Step 3: Saving the Function

After writing the function, the next step is to save it with an appropriate name and in an appropriate location. You can use the MATLAB editor to create, edit, and save your functions.

To save the function, click on the “Save” button in the editor and specify the name and directory where you want to save it.

Step 4: Testing the Function

Once you have saved the function, you can test it by calling it from the MATLAB command window. To call a function, you simply type its name followed by the input arguments in parentheses.

For our example function, we can test it by typing:

result = myAddition(2, 3)

This should return a result of `5`.

Step 5: Using the Function in Other Programs

Finally, you can use the function in other programs by simply calling it from the program. You need to make sure that the function is saved in a directory that is in the MATLAB search path.


To use the function in another program, you need to add the directory containing the function to the MATLAB path. This can be done by clicking on the “Set Path” button in the MATLAB Home tab and selecting “Add Folder”.

Once the directory has been added to the path, you can call the function from the program as follows:

result = myAddition(4, 5)

This should return a result of `9`.

Creating functions in MATLAB is an essential skill required for any programmer who wants to perform complex computations. By following these simple steps, you can create your own functions in MATLAB, making your code more modular and reusable.

That’s it!

Now that you know how to make a function in Matlab, you can start building amazing things in no time! Don’t forget to thank your teacher, and practice as much as you can. Remember that there’s always room for improvement, and with practice and patience, you can become a Matlab pro. Thanks for reading! Come back soon for more fun and useful topics!