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?
from numpy import loadtxtjust abovefrom numpy import *, do you see an error on that line?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.which pythonfrom your shell andsystem('which python')in MATLAB and compare the results. (If you're using Windows, I think the equivalent towhichiswhat).