How to get data from a "SimulationOutput" object from MATLAB into Python?

11 次查看(过去 30 天)
I have an instance of the MATLAB Engine in Python in a variable named "eng". I am using "eng" to simulate my Simulink model by calling it from Python. I am using the following Python script to get data from the "SimulationOutput" object created at the end of the simulation in MATLAB, in Python. However, I am not able to access any variables from the "SimulationOutput" object in Python. The "out" variable seems to only be a list.
out = eng.sim('modelName)
eng.workspace['variableListedInOut']
How can I resolve this?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2024-4-25
Only certain types of data types can be transferred from MATLAB to Python. For more information on how data types are mapped between MATLAB and Python, please check the documentation link below -
 
To actually achieve the workflow requested, you would first have to convert the "SimulationOutput" object to a struct in MATLAB and then access the data from that struct in Python. This can be achieved by using the script below 
eng.eval("out=sim('model_name')",nargout=0)
eng.eval("data = struct(out)",nargout=0)
data = eng.workspace['data']
The MATLAB "struct" object that is passed to Python will automatically be converted to a Python "dict" object. This will allow you to access the SimulationObject data. See this documentation page for more information about passing data from MATLAB to Python.
https://www.mathworks.com/help/matlab/matlab_external/handle-data-returned-from-matlab-to-python.html

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by