2

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

1

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please provide me with more explanation? Do you want me to do the following in the end of my Python code, import os os.system(str) where str is the code in your answer?
Thankyou for the updated solution. I checked the MATLAB help page that you have referred. Why do you use a \ after -sd as in -sd \"location_of...". Similalry there is a \ after -r as in -r \ "run.... From the MATLAB help page they do not use these slash. Thanks for any help.
The backslash is used to escape the " character. It's needed because otherwise subprocess.run wouldn't work properly.
I tried several times with minor variations but subprocess.run always causes error saying no such file or directory. Simply replacing subprocess.run in the given solution with os.system however works. Do not know if this is expected or weird but anyways work. I am using Python 3.7 on Ubuntu 16.04 LTS.
You have to add shell=True to the subprocess command to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.