How to get data from a "SimulationOutput" object from MATLAB into Python?
11 views (last 30 days)
Show older comments
MathWorks Support Team
on 10 Nov 2023
Edited: MathWorks Support Team
on 25 Apr 2024
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?
Accepted Answer
MathWorks Support Team
on 25 Apr 2024
Edited: MathWorks Support Team
on 25 Apr 2024
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 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!