필터 지우기
필터 지우기

An Error When Using MATLAB Engine API for Python

조회 수: 3 (최근 30일)
Jim Chung
Jim Chung 2022년 8월 19일
답변: Pratik Pawar 2022년 10월 4일
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.

답변 (1개)

Pratik Pawar
Pratik Pawar 2022년 10월 4일
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)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by