Hello, i want to stream the matlab/simulink scope in python to stream it lively. is it possible?

29 views (last 30 days)
i am running a model in the simulink.
i want the simulink plots/graph to run in the dashboard using streamlit. i wnat to use python code for this.
it is possible to run teh scope lively?

Answers (1)

Shantanu
Shantanu on 12 Sep 2025 at 10:11
Edited: Shantanu on 12 Sep 2025 at 10:11
Hi Archana,
You can get the Simulink scope data to Python, once the signal values are in python they can be plotted or streamed.
You can follow the below mentioned steps
1. Simulink Model setup
  • Open Simulink model
  • Add signal you want to stream to Byte Pack (Embedded Coder > Embedded Targets > Host Communication > Byte Pack)
  • Connect output of Byte Pack to UDP Send (Instrument Control Toolbox > UDP Send)
  • Configure Byte Pack by double clicking on it and set Data types to double
  • Configure UDP Send by double clicking and set:
Remote IP address to 127.0.0.1
Remote IP port to 5005
  • Set Stop Time to inf, this will make model run continuously until you manually stop it and live data can be fetched
2. Python Script
Create a new .py file, here is an example code snippet
import socket
import struct
# --- Configuration ---
# This IP and port MUST match the settings in the Simulink UDP Send block.
UDP_IP = "127.0.0.1" # IP address to listen on (localhost)
UDP_PORT = 5005 # Port to listen on
print(f"Starting UDP receiver on {UDP_IP}:{UDP_PORT}")
# Create a UDP socket
# AF_INET means we are using IPv4
# SOCK_DGRAM means we are using UDP
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the IP address and port
sock.bind((UDP_IP, UDP_PORT))
print("Receiver is running. Waiting for data from Simulink...")
# --- Main Loop ---
try:
while True:
data, addr = sock.recvfrom(1024)
# Unpack the received data.
# Simulink's Byte Pack block (set to 'double') sends 8 bytes.
if len(data) == 8:
value = struct.unpack('<d', data)[0]
# The [0] is because unpack always returns a tuple.
print(f"Received value: {value:.4f}", end='\n')
except KeyboardInterrupt:
print("\nReceiver stopped.")
sock.close()
You can run the Simulink model and the Python file, correspondingly you can see your terminal. You will now see a live stream of numbers from your Simulink model updating rapidly.
Some documentations for reference
  3 Comments
Walter Roberson
Walter Roberson on 14 Sep 2025 at 20:23
You want to stream a Simulink model through python.
There are three possibilities:
  1. You can use (interactive) MATLAB to run Simulink. Doing this requires a licence to run.
  2. You can use Simulink Compiler to create a compiled version of the model. Compiled versions of the model (effectively) do not require licenses to run. However, you need a license for MATLAB and Simulink Compiler in order to create the compiled model. (Note that Simulink Coder is not available for Student or Home licenses.) I am not sure whether models created by Simulink Compiler can use the python interface, but I believe they can
  3. You can use Simulink Coder to create a C/C++ version of the model. The C/C++ version will not itself have access to graphics, so all graphics would have to be done on the python end. You would need a license for MATLAB and MATLAB Coder and Simulink Coder in order to create the compiled model. (Note that Simulink Coder is available for Student licenses.) Models created by the Simulink Coder cannot use the MATLAB python interface, but are eligible for being called from python using whatever mechanism python provides for calling C++ code.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!