I'm calling a python script from matlab. The python script needs 3 arrays as input arguments:
import sys
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')
X = np.array(float(sys.argv[1]), dtype =np. float32)
Y = np.array(float(sys.argv[2]), dtype =np. float32)
Z = np.array(float(sys.argv[3]), dtype =np. float32)
scat = ax.scatter(X, Y, Z)
I call the Python script from Matlab like this:
!"MYPATH\python.exe" test3.py dX dY dZ
In Matlab, dX
, dY
and dZ
are all 1x500 array type. However, I get the following error:
ValueError: could not convert string to float: dX
It looks like the python script call doesn't evaluate the dX
array and takes the argument as a string. How can I correct that?
There is no straightforward way to pass array arguments to command line programs. Basically, all the command line arguments will always be interpreted as strings, broken into words. You could pass the arrays in the command line as separate entries, but there is a limit to the length of the command line. I would recommend that you save
the arrays in Matlab to a text file and then load them in the Python program:
In Matlab
filename = tempname;
data = [dX' dY' dZ'];
save(filename, 'data', '-ascii');
system(['"MYPATH\python.exe" test3.py "' filename '"']);
In Python:
dX, dY, dZ = np.loadtxt(sys.argv[1]).T
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.