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
-
Try this. mathworks.com/help/matlab/matlab_external/…gph– gph2020年06月18日 06:06:47 +00:00Commented Jun 18, 2020 at 6:06
1 Answer 1
You can use subprocess to start MATLAB from Python.
When you launch MATLAB, you should use the following startup flags:
sdTo set the MATLAB working directory.-rTo 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")
5 Comments
import os os.system(str) where str is the code in your answer?-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." character. It's needed because otherwise subprocess.run wouldn't work properly.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.shell=True to the subprocess command to make it work.