1. Overview

MATLAB is a popular numeric computing software used by engineers, scientists, and students. It makes performing matrix operations, plotting data, and implementing algorithms easier.

In addition to its basic capabilities, MATLAB has a rich set of toolboxes that extend its capabilities. Signal Processing, Parallel Computing, Financial, and Bioinformatics Toolboxes are just a few examples.

In this tutorial, we’ll discuss how to call MATLAB functions from the command line and then automatically return to the command line. First, we’ll define MATLAB functions and ways to call them from the command window. Then, we’ll see how to call functions from the Linux command line. Finally, we’ll look for solutions to automatically exit from the MATLAB command window after function calls.

2. MATLAB Functions

First, we’ll define a MATLAB function. Then, we’ll use this function both inside MATLAB from the MATLAB command window and outside MATLAB from the Linux command line.

2.1. Defining a MATLAB Function

Function definitions in MATLAB start with the function keyword, while the end keyword designates the function end.

First, let’s define the MATLAB function mysum() in the MATLAB file mysum.m:

function sum = mysum(n1, n2)
    sum = n1 + n2;
end

In this case, mysum.m is a function file. Function files contain only function definitions and their name must be the same as the first function in the file.

The name of the function in our example is mysum. It takes two inputs, n1 and n2n1 and n2 can be numbers, vectors, or matrices. Of course, the inputs must have the same dimension. For example, if n1 is a 2×3 matrix, then n2 must also be a 2×3 matrix.

The function returns the sum variable, which is obtained by adding n1 and n2.

2.2. Calling a Function Inside MATLAB

Now, let’s see how to call the mysum() function after starting MATLAB from the Linux command line.

First, we start MATLAB from the command line using matlab, which is a script installed with MATLAB:

$ matlab -nodesktop -nosplash
...
>> 

After some startup messages, the MATLAB command window appears, as indicated by the >> prompt. Then, we can run commands at that prompt.

The -nodesktop option starts MATLAB without the IDE, while -nosplash suppresses the display of MATLAB’s splash screen at startup.

When we start MATLAB from the directory where mysum.m resides, we can call mysum() directly:

>> mysum([1 2], [0 -1])
ans =
     1     1
>>

We add two 1×2 vectors using the statement mysum([1 2], [0 -1]). The vectors are [1 2] and [0 -1]. The result is stored in a variable named ans, which is the 1×2 vector [1 1], as expected.

If we don’t specify an output variable, MATLAB uses the ans variable for storing the output by default. Additionally, MATLAB prints the result automatically if we don’t end an expression with a semicolon.

2.3. Calling a Function Outside MATLAB

It’s also possible to call MATLAB functions from the Linux command line using the -r option of matlab:

$ matlab -nodesktop -nosplash -r "mysum([1 2], [0 -1])"
...
ans =
     1     1
>>

As the output shows, MATLAB starts and prints 1 1, which is the expected result.

Although we’re successful in calling mysum() from the Linux command line, there’s one issue. After we call the function, we aren’t back in the Linux command line. Instead, we’re inside the MATLAB command window. This implies that if we call a MATLAB function in a shell script, the shell commands after the matlab command aren’t executed until we exit from MATLAB manually using the exit or quit commands.

We’ll look for solutions to this problem in subsequent sections.

3. Using the quit Command of MATLAB

While calling a function from the command line using the -r option of matlab, we passed only mysum([1 2], [0 -1]) as the argument. However, it’s possible to pass more than one command as an argument.

Further, we can use the quit command of MATLAB as the last command to return to the Linux command line:

$ matlab -nodesktop -nosplash -r "result=mysum([1 2], [0 -1]); disp(result); quit"
...
     1     1
$

This time, we’re back in the Linux command line. The argument we pass using the -r option of matlab contains three commands in our example. First, we call the MATLAB function as before using result=mysum([1 2], [0 -1]). However, we assign the output of the function to a variable named result.

Then, we print the value of the result using the disp(result) command. The disp() command of MATLAB prints the value of the variable passed to it without printing the variable name.

Finally, we exit from the MATLAB command window using the quit command of MATLABWe can also use the exit command instead of quit.

4. Using the -batch Option

We can also use the -batch option to run MATLAB scripts or commands in batch mode without launching the MATLAB graphical user interface (GUI).

When we execute scripts with the -batch option, it runs and exits automatically.

Let’s use the -batch option to run the mysum() function:

$ matlab -batch "mysum([1 2], [0 -1])"
    1     1
$

The -batch option executes the specified script or command and then exits. This makes it useful for running MATLAB scripts in a non-interactive manner, such as in scripts, automated workflows, or on remote servers.

Additionally, we can also direct MATLAB output to a file for logging purposes:

$ matlab -batch "mysum([1 2], [0 -1])" > output.log 2>&1

This redirects both standard output and standard error to the output.log log file.

5. Using Input Redirection

It’s possible to run the MATLAB commands from a file by passing the file to matlab using input redirection.

For example, let’s explore matlab_command.m, which only has one function call:

$ cat matlab_command.m
mysum([1 2], [0, -1])

We used the cat command to display the content of matlab_command.m.

Let’s run the command mysum([1 2], [0, -1]) in this file using matlab:

$ matlab -nodesktop -nosplash < matlab_command.m
...
ans =
     1     1
$

As is apparent from the output, we’re successful in calling the mysum([1 2], [0 -1]) statement that’s stored in matlab_command.m.

If we want to call the same function with different parameters, it’s better to automate the process with a shell script instead of modifying matlab_command.m manually. We can use the following script for this purpose, namely mysum_parameters.sh:

$ cat mysum_parameters.sh
#!/bin/bash

echo "mysum(${1})" > matlab_command.m
matlab -nodisplay -nosplash < matlab_command.m

This script writes the output of the echo “mysum(${1})” command to matlab_command.m using output redirection. The first parameter passed to the script, namely ${1}, should contain the arguments to mysum(). Then, we execute the statements in matlab_command.m using matlab and input redirection as before.

Let’s call the script to add two integers this time, 2 and 23:

$ ./mysum_parameters.sh "2, 23"
...
ans =
     25
>>
$

The result is 25, as expected.

6. Conclusion

In this article, we discussed how to call MATLAB functions from the Linux command line and then again return to the command line automatically.

First, we defined a simple MATLAB function and called it from the MATLAB command window. Then, we called the same function from the Linux command window. However, we saw that after calling the function, we were still in the MATLAB command window.

Thus, we used the quit command of MATLAB to return to the Linux command line as the first solution. Then, we explored how to use the -batch option to automatically exit after execution. Finally, we saw that we could call the function from the command line by passing it in a file to MATLAB using input redirection.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.