simulink output to workspace every second

8 visualizaciones (últimos 30 días)
nayong kim
nayong kim el 22 de En. de 2019
Respondida: Riya el 3 de Jun. de 2025
After running the current Simulink model, data will be sent to the workspace through the 'simout' block after the simulation is finished.
Simulink to design a model that should be in real-time transmission over tcp / ip communication
If the Simulink model needs to export data to the workspace during run time every second,
Do you have the necessary blocks or logic?
please help me,,!

Respuestas (1)

Riya
Riya el 3 de Jun. de 2025
Hi Nayong,
I understand that you want to export Simulink output data to the MATLAB workspace every second during simulation run time, rather than only at the end, and then use that data for real-time transmission over TCP/IP.
You can refer to the following approach for the same:
Use a MATLAB Function block to update workspace variables every second. Basically, write a function inside Simulink that assigns the signal value to a variable in the base MATLAB workspace periodically during simulation. You can do this with a persistent timer and assignin command. Kindly refer to the following sample code:
function y = pushToWorkspace(u, t)
% u: input signal, t: current simulation time
persistent lastTime
if isempty(lastTime)
lastTime = -1;
end
if t - lastTime >= 1 % Update every 1 second
assignin('base', 'latestData', u);
lastTime = t;
end
y = u; % pass through the signal unchanged
end
Here, you need to connect your signal to u input and also connect a Clock block to input t to get the current simulation time. After running the simulation, you can access the variable latestData in the MATLAB workspace, which updates every second.
Alternatively, you can use MATLAB scripts to read workspace variables updated by the Simulink model and then send data using tcpclient. Also, Simulink’s Instrument Control Toolbox supports TCP/IP blocks for sending data.
For reference, Refer to the following MATLAB documentation:

Categorías

Más información sobre Event Functions en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by