## MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

In this article, you will learn how to define a MATLAB function from scratch. This article is meant for beginners who don’t know anything about MATLAB functions.

I’ll show you everything that you need to know about:

- How to write a function in MATLAB
- How to call a function in MATLAB
- Multiple input/output MATLAB functions
- Matrix functions
- Vector functions
- MATLAB subfunctions: multiple functions in the same .m file

## How to Write a Function in MATLAB

First of all, if you want to create a MATLAB function, you will have to create a new script (a .m file in which you will have to write the definition of your function). To do so, follow these 2 steps:

- Go to the top left of your MATLAB window, under the “
**HOME**” tab - Click on
**“New Script**“:

This will open up the MATLAB editor, which is where you will write your function.

**The name of the .m file**that you opened**has to be the name of the function**that you are defining.

## Define a MATLAB Function: The Syntax

Here is the simplest way to define a MATLAB function:

function out = functionName(in) instructions end

In this example, you have one input and one output for the function.

- To define a function, you basically have to write the keyword
**function**, then your outputs, the symbol “=” followed by the name of your function and your input in between parenthesis. - Then, you just need to add the keyword
**return**to indicate the end of your function.

## How to Call a Function in MATLAB

- You can call a function in MATLAB by just writing its name in your workspace as follows:
out = functionName(in);

This will define the variable “out” in your workspace.

- You can also call a function from a different .m file in the same way, but this function has to be either in your current folder, or you have to add the path to the folder that contains your function using the MATLAB command
**addpath:**addpath('path/to/your/function');

- You can also call a function from the same file (a script or a function). We will see how to do that at the end of this article.

## Multiple Inputs and Outputs Function

Sometimes, you will have to define functions with several inputs and outputs, in which case you can do the following:

function [out1, out2, out3] = functionName(in1, in2) instructions end

**Outputs**: you just need to list every output of your function separated with commas in between square brackets.**Inputs:**list every one of them separated by commas and enclosed by parenthesis.

Note that if you want to use a function with multiple outputs, you don’t need to define every output. Instead, you can use the tilde symbol (~).

For example, if you want to access the third output without accessing the second:

[out1, ~, out3] = functionName(in1, in2)

This is helpful if you don’t want to define the second output of your function when you’re using it. (This can be useful if you don’t want to get confused by defining unnecessary variables.)

## MATLAB Matrix Function

Sometimes, you will also need to pass a matrix as an input or as an output of your function. In that case, you’ll have to use them as arguments as if it was a single input/output function.

For example, here’s a function that has a single input matrix and a single output matrix. Let’s say you have the following matrix :

A = [1 2 ; 3 4]

And you want to add this matrix to the matrix A:

B = [0 1 ; 0 0]

This would be the function to do so:

function APlusB = sumMatrices(A) B = [0 1 ; 0 0]; APlusB = A + B; end

If you try this in MATLAB, you’ll get:

Now, what if you want several inputs and several outputs matrices for your function? You can just define them exactly as if they were scalar variables.

For example, if you want the previous function to have both matrices as an input and have a single output that calculates the sum of those two matrices this is the function to do so:

function APlusB = sumMatrices(A, B) APlusB = A + B; end

## MATLAB Vector Functions

What if you want to use vectors as input or output? Well, keep in mind that vectors can be considered matrices that have either one column or one row. Basically, vector functions work exactly as matrix functions.

This is an example of a function that computes the dot product of two vectors:

function xDotY = dotproduct(x, y) xDotY = x*y'; end

If you define the two vectors:

x = [1 2] y = [3 4]

You get

## MATLAB Subfunction: Multiple Functions in One File

If you want your code to be readable, clean, and organized, I highly recommended using multiple functions in the same file.

The general rule of thumb is “one function does one thing,” which means that if you want to create a function that does several things, you’ll have to create several functions. To do That, you’ll need one main function and several subfunctions that will be called by the main function.

So, how do you define the main function?

- The
**main function**is the function that has the name of the .m file. For example, let’s say you have a list (or a vector) of strings and you want to add some text at the end of every string of that list. You can then define one function with a for loop that will be based on the number of strings in your list. - In that function, you’ll call a
**subfunction**that will add some text at the end of your list. For example, let’s say that the main function is called addSuffixToList, and the subfunction is called addSuffixToString. The name of the script that you create will have to be called addSuffixToList.m (i.e., the name of your main function).

Here’s how to define that function with its subfunction (for more information about lists and matrices of strings, see MATLAB: Mixing Strings and Numbers in a Matrix) :

function listWithSuffix = addSuffixToList(list) for i = 1:numel(list) listWithSuffix{i} = addSuffixToString(list{i}); end end function stringWithSuffix = addSuffixToString(string) suffix = '_addition_info'; stringWithSuffix = [string suffix]; end

You also could use this function as the following:

list = [{'cost'} {'width'} {'length'}]; addSuffixToList(list)

You would then get

**Key takeaways:**

- To Write a function in MATLAB:
- Go to the top left of your MATLAB window, under the “
**HOME**” tab - Click on
**“New Script**“

- Go to the top left of your MATLAB window, under the “
- Define a MATLAB function using the following syntax:

function [out1, out2, out3] = functionName(in1, in2) instructions end

- Define a matrix function or a vector function by considering the matrices and vectors as variables:

function [matrix1, matrix2] = functionName(vector1, vector2) instructions end

- Write multiple functions in one file
**by naming your script with the name of your main function**.

For more information you can also refer to the MATLAB documentation: Declare function name, inputs, and outputs – MATLAB function.