import from python to simulink, matlab.eng​ine.Matlab​ExecutionE​rror: Invalid Simulink object name: try11/Constant2.

A very strange error, When I try to run this code in Matlab m code files (action1 and action2 are predefined:
% action1=1
% action2=2
set_param('try11/Constant2','Value','action2')
set_param('try11/Constant','Value','action1')
sim('try11.slx')
s1=try11/Constant
no error is reported and the simulation is started. try11 is just a simple Simulink file adding "Constant2" and "Constant". However when I try to run the following in Python.
import matlab.engine
eng = matlab.engine.start_matlab()
eng.workspace['action2'] = matlab.double([0.0])
eng.workspace['action1'] = matlab.double([1.0])
eng.tryy(nargout=0)
reward = eng.eval('ans.output')
print(reward)
I get the following error in python Console:
Error using tryy (line 3)
Invalid Simulink object name: try11/Constant2.
Caused by:
Error using tryy (line 3)
The block diagram 'try11' is not loaded.
Traceback (most recent call last):
File "/home/majid/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-06fc4fb2fc97>", line 1, in <module>
runfile('/home/majid/PycharmProjects/reinforcementlearning/Main_Directory/mattry.py', wdir='/home/majid/PycharmProjects/reinforcementlearning/Main_Directory')
File "/snap/pycharm-professional/218/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/snap/pycharm-professional/218/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/majid/PycharmProjects/reinforcementlearning/Main_Directory/mattry.py", line 13, in <module>
eng.tryy(nargout=0)
File "/home/majid/anaconda3/lib/python3.8/site-packages/matlab/engine/matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "/home/majid/anaconda3/lib/python3.8/site-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/home/majid/anaconda3/lib/python3.8/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Invalid Simulink object name: try11/Constant2.

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品

版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by