changing constant block of simulink (Matlab) through python

Viewed 171

when I define a variable to be used in Simulink in an m file (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"(action2) and "Constant" (action1). However when I try to run the following in Python.

    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.

        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 Answers
Related