MATLAB: UDP connection in two instances of Matlab

MATLABtwo instancesudp

Hi, I'm trying to sendi information between 2 instances of Matlab running at the same time. I've been able to use UDP in the past, but this doesn't seem to work.
% I would be sending info through instance A (integers with varying sizes each time, but only one integer at a time)
u = udp('192.168.1.1', 2011, 'LocalPort', 2012);
fopen(u);
data = 1000;
fwrite(u, data);
And then waiting for the data to arrive using another instance of matlab:
% and receive data on instance B
u = udp('192.168.1.1', 2012, 'LocalPort', 2011);
fopen(u);
signal = [];
while isempty(signal) == 1 % pooling until signal is received
signal = fscanf(u,'%d');
end
What am I missing?
I would also ideally need to send a message from the first instance to be read by the second instance later.
So Instace A would give me the number I need and send it as soon as it gets it. Meanwhile, instance B might be doing another operation and not able to receive the data instantly, but with a delay.
I have already made sure the ports are not block by the windows firewall udp tcp/ip connection.
How can we do that?
Thanks!

Best Answer

You are fwrite() of the data, which writes binary. You are fscanf() of the data, which expects text.
fprintf(u, '%d\n', data);
Related Question