Clear Filters
Clear Filters

How can I receive a cell array using a UDP port?

7 views (last 30 days)
I have the following code where I am sending a cell array containing numeric and string values. I want to receive and read this cell array on another client. How can I modify the code to achieve this?
client_port = 10011;
clientAddress = '';
% Define the cell array
C = {5 "cereal" 110 "C+"; 12 "pizza" 140 "B";...
23 "salmon" 367 "A"; 2 "cookies" 160 "D"};
% Open a UDP port
u2 = udpport("IPV4",'LocalPort',client_port);
for ii = 1:numel(C)
write(u2, C{ii}, class(C{ii}), clientAddress, client_port);

Accepted Answer

Hassaan on 4 Mar 2024
Edited: Hassaan on 4 Mar 2024
client_port = 12389;
clientAddress = '';
% Define the cell array
C = {5, "cereal", 110, "C+"; 12, "pizza", 140, "B";...
23, "salmon", 367, "A"; 2, "cookies", 160, "D"};
% Convert the cell array to JSON string
C_json = jsonencode(C);
% Open a UDP port
u2 = udpport("IPV4", 'LocalPort', client_port);
% Send the JSON string every 1 second
while true
% Send the JSON string
write(u2, uint8(C_json), "uint8", clientAddress, client_port);
disp('Data sent..');
% Pause for 1 second before next send
% Example port and IP setup for the receiver
server_port = 12389; % The port where the server listens
% Open a UDP port for receiving
u = udpport("IPV4", 'LocalPort', server_port);
% Loop until data is received
while true
% Check if data is available
if u.NumBytesAvailable > 0
% Read the available data
data = read(u, u.NumBytesAvailable, "uint8");
% Convert uint8 data to string
data_str = char(data');
% Decode the JSON string back to cell array
C_received = jsondecode(data_str);
% Display the received cell array
% Break the loop after processing the received data
% Optional: pause for a short time to avoid busy waiting
This code assumes that the receiving client is set up and ready to receive data sent to its LocalPort. Adjust the server_port and any other necessary configuration to match your setup.
Note: The effectiveness of this method depends on the size of your cell array and the limitations of your UDP buffer sizes. Large data might need segmentation and reassembly logic.
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
Walter Roberson
Walter Roberson on 5 Mar 2024
I would check whether data_str is 1 x N or N x 1

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!