我有一个名为“calculate_K_matrix.m”的matlab函数文件 '包含以下代码:
function K = calculate_K_matrix(A, B, n)
K = place(A, B, eigs(A)*n)
end
我可以这样从matlab中调用它:
^{pr2}$但当我尝试使用matlab引擎API从python调用此函数时,如下所示:
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
A = matlab.double([[0, 1],[-100, -5]])
B = matlab.double([[0],[7]])
n = 1.1 double(param initializer=None, param size=None, param is_complex=False)
n_matlab = matlab.double([n])
eng.addpath(r'/home/ash/Dropbox/SimulationNotebooks/Control')
K = eng.calculate_K_matrix(A, B, n_matlab)
然后我得到以下错误:
In [17]: run test.py
Attempt to execute SCRIPT calculate_K_matrix as a function:
/home/ash/Dropbox/SimulationNotebooks/Control/calculate_K_matrix.m
---------------------------------------------------------------------------
MatlabExecutionError Traceback (most recent call last)
/home/ash/Dropbox/SimulationNotebooks/Control/test.py in <module>()
10
11 eng.addpath(r'/home/ash/Dropbox/SimulationNotebooks/Control')
---> 12 K = eng.calculate_K_matrix(A, B, n_matlab)
/home/ash/anaconda2/envs/python3/lib/python3.5/site-packages/matlab/engine/matlabengine.py in __call__(self, *args, **kwargs)
76 else:
77 return FutureResult(self._engine(), future, nargs, _stdout,
---> 78 _stderr, feval=True).result()
79
80 def __validate_engine(self):
/home/ash/anaconda2/envs/python3/lib/python3.5/site-packages/matlab/engine/futureresult.py in result(self, timeout)
66 raise TypeError(pythonengine.getMessage('TimeoutCannotBeNegative'))
67
---> 68 return self.__future.result(timeout)
69
70 def cancel(self):
/home/ash/anaconda2/envs/python3/lib/python3.5/site-packages/matlab/engine/fevalfuture.py in result(self, timeout)
80 raise TimeoutError(pythonengine.getMessage('MatlabFunctionTimeout'))
81
---> 82 self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
83 self._retrieved = True
84 return self._result
MatlabExecutionError: Attempt to execute SCRIPT calculate_K_matrix as a function:
/home/ash/Dropbox/SimulationNotebooks/Control/calculate_K_matrix.m
我如何解决这个问题?在
使用getattr,例如:
我就是这样做的:
^{pr2}$图片: Resultfunction
相关问题 更多 >
编程相关推荐