How to Exchange Real-Time Data Between Two Computers Using MATLAB?
Show older comments
Hello everyone,
I am working with two MATLAB scripts running on two different PCs in the same network. I want to establish a connection between them so that they can exchange data in real time while both are running.
The goal is to send data continuously from Computer A to Computer B, and vice versa, with minimal latency.
Could you please advise on the best approach to achieve this?
Accepted Answer
More Answers (1)
Walter Roberson
on 22 Aug 2025
0 votes
The general mechanism is to use the Instrument Control Toolbox udpport. udp is a connectionless bi-directional protocol
If you do not have the Instrument Control Toolbox, you can use TCP/UDP/IP Toolbox from the File Exchange. That is an older toolbox, and it is not immediately clear whether it is still compatible with modern MATLAB.
udp is not high latency; packets are generally sent towards the destination soon after the packets are created.
udp is considered "unreliable". It is legal for udp packets to be dropped by anything along the line, and there will be no notification of dropped packets. It is also possible for udp packets to arrive out-of-order. udp packets are considered to be unordered; it is entirely possible for the received relative packet order to be 3 1 4 5 7 6 (with packet 2 never received.) Because of this, it is common for the programmer to add sequence numbers to the packets, to at least be able to detect missing or disordered packets, and is it common to make packets match up to complete "updates".
The main alternative it tcp. tcp is considered ordered. Sequence numbers are built into the TCP protocol, and the protocol itself will detect missing packets and will automatically send a request for retransmission. Because tcp packets might potentially get lost on the way (they will not be deliberately dropped unless dropped by security policy), it can end up being a while before resyncrhonization is finished, and there is nothing in the protocol that might correspond to "Never mind the lost packets, continue from here." Because of the automatic sequence numbering and automatic reordering of received packets, it is common for arbitrary streams of data to be sent across tcp, without paying attention to packet boundaries -- tcp automatically fragments larger blocks of data into packets.
If you have a low to medium timeout on receiving packets, together with logic to handle missing packets, then you need udp. As the timeout becomes indefinitely long then the advantages of TCP start looking very attractive.
1 Comment
Walter Roberson
on 22 Aug 2025
Oh wait, in R2019a you would use https://www.mathworks.com/help/instrument/udp.html not the newer udpport
Categories
Find more on Development Computer Setup 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!