0

I have a MATLAB function that needs to communicate (not rapidly, and not often) with python code. I have MATLAB write numbers to a file. Python reads the file, does some calculations, and writes some results to another file. MATLAB then reads that file and continues on its way.

The problem I am having is when I want to execute the python script from MATLAB. I have found in the past that simply performing a system call within MATLAB has been sufficient:

system('python myscript.py')

However, it seems to not like the numpy function 'loadtxt' when it reaches that point in the python script:

NameError: name 'loadtxt' is not defined

I am defining it (from numpy import *). If I just execute the script from terminal, it reads in the file using loadtxt just fine. It is only when I execute the script using the system call do I get that error. The python .py file is in the same directory as the MATLAB .m file that is making the system call. I have executed other scripts without loadtxt just fine using this same method.

Any suggestions?

5
  • If you add a from numpy import loadtxt just above from numpy import *, do you see an error on that line? Commented Feb 15, 2011 at 17:12
  • 1
    import * is generally frowned upon because it causes namespace collisions. Also you might want to check the Python and NumPy versions that you are using with MATLAB vs. terminal. Commented Feb 15, 2011 at 17:14
  • 3
    Try executing which python from your shell and system('which python') in MATLAB and compare the results. (If you're using Windows, I think the equivalent to which is what). Commented Feb 15, 2011 at 17:38
  • @kwatford, the results are different. Can someone explain how I can change the path such that they are the same? Is there a way I can invoke a system-wide change to always use one version of python? Commented Feb 15, 2011 at 17:56
  • system('which python') gives /usr/bin/python and system('echo $PYTHONPATH') gives /Library/Frameworks/Python.framework/Versions/Current/bin/python2.7 Commented Feb 15, 2011 at 18:53

2 Answers 2

3

It looks like you're using OS X. It is highly advisable not to attempt to screw around with the global definition of what python interpreter to use in OS X.

You can probably just call the desired interpreter directly. So instead of calling system('python myscript.py'), get the desired interpreter's full path into a variable and call:

system([use_this_python ' myscript.py'])

or something similar.

Exactly how you want to determine what python interpreter to use is between you and whoever is using this. Perhaps have it just call "python" by default, but allow the user to specify some arbitrary interpreter via config file.

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

Comments

0

The shell on your system and the shell opened by Matlab when you execute the system command are not necessarily the same.

Importantly, there could be different environment variables. Try e.g. executing system('echo $PATH') to check whether you have access to all your libraries.

Comments

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.