simulink output to workspace every second

조회 수: 8 (최근 30일)
nayong kim
nayong kim 2019년 1월 22일
답변: Riya 2025년 6월 3일
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,,!

답변 (1개)

Riya
Riya 2025년 6월 3일
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:

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by