Sending data via udp. Would appreciate help with the proper way of converting a cell array containing char, string, double to ASCII data

3 views (last 30 days)
Hello!
I have to send the information contained in the attached file DATA.mat to another program (it's running on the same PC for testing). I'm using an UDP-Client/Server and the main program expects the following format (pause is used because the data is normally generated continuously) :
DATA {1,1} \t DATA {1,2} \t ... \t DATA {1,30} \n
...
DATA {end,1} \t DATA {end,2} \t ... \t DATA {end,30} \n
I have managed to do this but it is so ugly that I might get a stroke. I would be very happy if somebody can show me a better way to do the '%convert' part!
My code:
if ~isempty(instrfindall)
fclose(instrfindall);
end
load('DATA.mat')
udpMonitor = udp('localhost',9098,'LocalPort',9099, 'InputBufferSize',100000);
fopen(udpMonitor);
[index5_max, ~] = size(DATA);
for index5= 1:index5_max
%convert
AnMonito= string(DATA(index5,:));
AnMonito= strcat(AnMonito(1)," ",AnMonito(2)," ",AnMonito(3)," ", ...
AnMonito(4)," ",AnMonito(5)," ",AnMonito(6)," ",AnMonito(7)," ",AnMonito(8)," ", ...
AnMonito(9)," ",AnMonito(10)," ",AnMonito(11)," ",AnMonito(12)," ",AnMonito(13)," ",AnMonito(14)," ", ...
AnMonito(15)," ",AnMonito(16)," ",AnMonito(17)," ",AnMonito(18)," ",AnMonito(19)," ",AnMonito(20)," ", ...
AnMonito(21)," ",AnMonito(22)," ",AnMonito(23)," ",AnMonito(24)," ",AnMonito(25)," ",AnMonito(26)," ", ...
AnMonito(27)," ",AnMonito(28)," ",AnMonito(29)," ",AnMonito(30));
AnMonito=convertStringsToChars(AnMonito);
%sent
fwrite(udpMonitor, [double(AnMonito) 10]);
pause(0.1);
end
fclose(udpMonitor);

Accepted Answer

Guillaume
Guillaume on 30 Jul 2019
strData = string(DATA);
for row 1:size(strDATA, 1)
strrow = strjoin(strDATA(row, :), '\t') + char(10);
fwrite(updMonitor, char(strrow)); %Maybe you can pass string directly to fwrite. If not pass char. Converting to double is a waste of time.
end

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!