1

I need to run a MATLAB script from a Python script. I don't care about the output of it nor do I need to give it any arguments.

However, MATLAB R2016B's "engine" does not support Python 3.7 (Upgrading Matlab or down-grading python is not an option at this time)

So, I decided to make a shell script that runs it:

#!matlab -nodisplay -nodesktop -r 'try; myMatlabScript; catch; end; quit'

Now I need to run a bash script from Python. To do so, I did:

import subprocess
subprocess.call("./mybashscript.sh")

(And yes, the python script is at the same level as the shell script)

The python script does not complain directly. However, I do get the following:

'.' is not recognized as an internal or external command, operable program or batch file.

Which to me means that since Windows doesn't directly have bash, it doesn't know what to do with this shell script. I am not sure how to handle this. Some way to tell Python to use MSYS instead of Windows for the shell?

And thus the MATLAB script does not appear to run at all.

When I attempt under Linux (just for testing, I can't run it here for performance reasons), I get:

./mybashscript.sh: matlab: bad interpreter: No such file or directory

Is it possible this is because I didn't do the command addpath(genpath('.'))? If so, I'm not sure how I would do that in the shell script, and some help would be appreciated.

Or some other better solution would also be great.

1 Answer 1

1

1: Needed to re-name mybashscript.sh to mybashscript.bat

2: Needed to change the sub-process call to subprocess.call("mybashscript.bat") (as ./ was confusing the windows shell)

3: Needed to add the path properly. Here is what the batch script looked like:

matlab -nodisplay -nodesktop -r "addpath(genpath('C:/path/to/myscript')); myMatlabScript"

The double quotes are neccesary so the single quotes inside genpath do not cause it to end early.

And that was it!

EDIT: You can add -wait in the batch file to get the script to wait until it is complete before handing back to the Python script.

Sign up to request clarification or add additional context in comments.

9 Comments

You can also do `-r "run('C:/path/to/myscript/myMatlabScript')".
@CrisLuengo Thanks! I do have one other question. Do you know if there is any way to get the shell script (or the python script) to wait until the MATLAB script is complete?
@CrisLuengo There is one other thing. Paradoxically enough, I get different results when running the script manually vs from the Python script. They're run over identical inputs, it's very odd.
Different results? Are you running the same version of MATLAB? Are you running the exact same script? Does the same startup.m file get executed when starting up MATLAB?
As far as I am aware, I only have one installation. And yes, exact same script. How would I check the startup.m being used?
|

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.