An Error When Using MATLAB Engine API for Python

9 Ansichten (letzte 30 Tage)
Jim Chung
Jim Chung am 19 Aug. 2022
Beantwortet: Pratik Pawar am 4 Okt. 2022
Hello all,
I want to run a MATLAB Simulink model in Python. The Python environment is 3.7.9, while the Simulink is 2020a. The Python API was also installed properly. And the model and the code are in the same folder.
import matlab.engine
#Establish a connection
print("Starting MATLAB")
eng = matlab.engine.start_matlab()
print("Connected to MATLAB")
#Load the model
env_name = 'case1'
eng.load_system(env_name)
Thanks.

Antworten (1)

Pratik Pawar
Pratik Pawar am 4 Okt. 2022
Please note that Anaconda is not supported, and is not guaranteed to work. The supported Python Implementation can be found through:
Python Engine simply launches MATLAB, passes requests to MATLAB and retrieves results. Python Engine is a bridge between Python and MATLAB, it doesn't know what's happening in MATLAB. It is up to MATLAB regarding things like MATLAB Path.
As a possible workaround, try following things
  • Add the path using python engine
eng.addpath(path_to_Simulink_model)
  • If the addpath does not work, create pathdef.m in the working directory and add all the wanted pathes.
If you want to open Simulink model then use the following
eng.open_system(env_name,nargout=0)
If you don't want to open the model, just load the system and run it
eng.load_system(env_name)
results = eng.sim(env_name)
eng.close_system(env_name, nargout=0)

Kategorien

Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by