Mohit Lamba
Mohit Lamba

Reputation: 1403

Run MATLAB script from Python

I am accessing a remote system and the way to launch MATLAB is to execute the following after doing ssh on ubuntu terminal.

/media/data/software/matlab2018a/bin/matlab -nojvm -nodisplay -nosplash

which opens a simple vi kind interface of MATLAB without opening the full GUI.

/media/data/software/matlab2018a/bin/matlab is the location of my MATLAB exe file and -nojvm -nodisplay -nosplash basically tells not to open full MATLAB GUI as working on remote desktop.

Now when I am inside the MATLAB editor I run the following to execute the MATLAB script script_name

cd location_of_script
script_name

script_name basically reads some images generated by Python, does some processing and saves the results in some .txt file.

I want to unify this two step procedure. That is as soon as my Python function is done with its job it should call the MATLAB script and terminate only when .txt file is saved.

The script does not require any input parameter.

Thankyou

Upvotes: 2

Views: 2043

Answers (1)

Paolo
Paolo

Reputation: 26230

You can use subprocess to start MATLAB from Python.


When you launch MATLAB, you should use the following startup flags:

  • sd To set the MATLAB working directory.
  • -r To execute the statement which MATLAB will run.

More info on the startup options here.

import subprocess
subprocess.run("/media/data/software/matlab2018a/bin/matlab -sd \"location_of_script\" -r \"run('script_name');exit\" -nojvm -nodisplay -nosplash")

Upvotes: 2

Related Questions