How To Call a Function in MATLAB

Knowing the structure, inputs, outputs, and control flags of each function we use in our program, whether in MATLAB or another programming language, makes a difference in the quality and quantity of the code.

This Linux Hint article explains function calls, their structure, and input and output arguments in MATLAB.

Next, we will look at some practical examples with different functions. We will add fragments and code images showing the different modes of function calling in the MATLAB environment.

What is a Function in MATLAB?

A function is a fragment of code. When a function is called, the program flow jumps into that code, executes it by performing a specific task or data processing, and then returns with a result to the following code line from which the function was called. These codes can be part of MATLAB’s extensive library, but we can also create our function libraries.

A function is divided into Name, Input Arguments, and Output Arguments. The following is the structure or syntax of a function in MATLAB:

Output Arguments = Name ( Input arguments );

Below, we describe what each of these parts making up the structure of a function is about:

Name: The name of the function and the one used to call it.

(input arguments): This is the data sent when the function is called. This data can be scalar matrices, vectors, 2D, and multidimensional or vector cell matrices. They can also control flags of the function’s various options for processing input or output data.

The input arguments should always be enclosed in parentheses, as shown below:

Name ( Input Arguments );

In cases where there is more than one input argument, they must be separated by commas and specified in the order given by the syntax of the respective function.

output = Name ( input-arguments , input-argument )

Output arguments: These are the results that the function returns when it returns from the call. These output arguments can be in the form of scalars, vectors, 2D, and multidimensional arrays. In cases where the output arguments are more than one, they must be enclosed in square brackets and separated by commas, as shown in the following syntax:

[ output-arguments, output-arguments... n] =Name ( input-arguments).

Calling Functions From the MATLAB Command Console

The command console of the MATLAB environment allows us to call functions from there. So, it’s an excellent resource for learning how to use and call functions because we can run code from there and see the results in real-time. The following image shows this tool executing functions and displaying their output in MATLAB:

How To Call a Function in MATLAB

To call a function, we need to write in a line of code in the following order, output argument, followed by the “=” character, function name, and then the input arguments in parentheses.

When we want to use a function, we need to be clear about what it does, what data we want to process, and where we want to store the results. Below, we see some examples of calling different functions.

How To Call Functions, Process Data, and View Results in MATLAB

The most commonly used functions in MATLAB are those sent with data and return a result. In this example, we call the function “sqrt()”, which returns the square root of “x” in “a”, which in this case is a scalar that we created earlier and assign an arbitrary value to. Below, you can see the code snippet for this:

x = 9;

a = sqrt ( x )

Copy and paste this snippet into a script or command line. When you run it, create the scalar “x” and call the sqrt() function with “x” as input and “a” as output. The result can be seen in the MATLAB console.

In the following excerpt, the input “x” for sqrt() is the result of the function “randi()”. This function returns a random value in the number range specified in its input argument, which, in this example, is 100.

x = randi ( 100 )

a = sqrt ( x )

The result is a random value in “x” and the square root of this value in “a”.

How To Nest Functions or Operations in Input Arguments With MATLAB

If the input arguments to a function result from an expression or function, you can nest those arguments. The following is the code from the previous example, but with the randi() function nested within the arguments of sqrt():

a = sqrt ( randi ( 100 ) )

The function assumes that its input argument results from the nested function. The same is true for any kind of expression.

The following snippet shows that the input argument of sqrt() results from a nested expression:

b = 2;

c = 7;

a = sqrt ( (b + c) )

Calling Functions From the Expression of a Conditional

Also, in MATLAB, it is possible to call a function from a conditional expression, and its result is the condition. This is as convenient as nesting functions because it saves lines of code and increases readability. Here is an example where we use the condition “if”, and the condition is the result of the function isnumeric():

x = 2;

if isnumeric ( x )


How To Call Functions With Multiple Output Arguments in MATLAB

In MATLAB, some functions return with multiple arguments. These must be enclosed in square brackets before the “=” sign, separated by commas and in the order given by the syntax of the function in question.

In the following example, we call the min() function, which returns a vector with the minimum value of each row of its input array and another vector with each value’s position index. Below, we see the code of this example where we create the array “x” with random values generated by the randi() function nested in min(). The output arguments are “minimum” and “index”. The results can be seen in the command console.

[ minimum, index ] = min ( randi ( 100, 5 ) )

The figure shows that the function min() returns two vectors, minimum and index, with their respective results when called.


In this article, we have explained everything you need to know to master the technique of MATLAB functions. We have also shown it as a function in MATLAB, its input and output structure, and the syntax for each calling method. In addition, we include practical examples of calls with code fragments and images showing the different methods for function calls in the MATLAB environment. We hope that this MATLAB article was helpful for you. See other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).