Python Matlab engine - Cannot read simulink output
Ältere Kommentare anzeigen
I'm trying to read the output of a Simulink simulation through Python with Matlab Engine but I'm having trouble as I am not able to get the output data.
I've seen this post : https://www.mathworks.com/matlabcentral/answers/725147-data-from-simulink-to-python-with-to-workspace-block?s_tid=answers_rc1-1_p1_Topic
But it doesn't work for me.
calling sim() does not add the output to the workspace and the object it returns has no usable properties (except size)
Python 3.6.4. Matlab 2020b
Thank you
import matlab
import matlab.engine as matlabEngine
import math
import numpy as np
import time
class LogTime:
def __init__(self, action):
self.action = action
def __enter__(self):
print('%s...' % self.action, end='', flush=True)
self.time_start = time.time()
def __exit__(self, *args, **kwargs):
print('Done in %0.3f seconds' % (time.time()-self.time_start))
# Params
modelname = 'simpleModel'
stopTime = 1;
nbPoints = 10000;
simstep = 0.0001;
with LogTime('Starting Matlab Engine'):
m = matlabEngine.start_matlab();
t = np.linspace(0,stopTime,nbPoints)
voltage = 120*math.sqrt(2)*np.sin(2*math.pi*60*t)
current = 120*math.sqrt(2)*np.sin(2*math.pi*60*t + math.pi/8)
# Numpy to matlab conversion
t = matlab.double(t.tolist())
voltage = matlab.double(voltage.tolist())
current = matlab.double(current.tolist())
# Write to workspace for model
m.workspace['input_voltage'] = m.timeseries(voltage, t)
m.workspace['input_current'] = m.timeseries(current, t)
with LogTime('Loading Model'):
m.load_system(modelname, nargout=0)
m.set_param(modelname, 'StopTime', str(stopTime), 'FixedStep', str(simstep), nargout=0)
with LogTime('Running simulation'):
out = m.sim(modelname) # Empty object. don't know what to do with it.
Akzeptierte Antwort
Weitere Antworten (1)
Sean de Wolski
am 6 Dez. 2021
0 Stimmen
Extract the simout to a timetable, then pass the timetable back to python either via writing to disk as a CSV, or converting to a struct (timetable2table(table2struct()) directly back to python.
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!