title: Communicating between Pure Data and MATLAB using OSC
body: <p>I'm trying to receive a message sent over OSC from Pure Data (or Max/MSP) to MATLAB.</p>
  
  <ul>
  <li>I'm able to send OSC messages from Pure Data to Pure Data</li>
  <li>I can send messages from MATLAB to MATLAB</li>
  <li>I can even send messages from MATLAB to Pure Data</li>
  <li>...I'm really struggling to get Pure Data to talk back to MATLAB</li>
  </ul>
  
  <p>Here's my code that sends messages from MATLAB (I'm using the oscmex protocol):</p>
  
  <pre><code>host = 'localhost'; % local host UDP address
  sendPort = 3333; % UDP port number to send over
  receivePort = 3333; % UDP port number to receive from
  oscAddress = osc_new_address(host, sendPort); % open send address
  oscServer = osc_new_server(receivePort); % open server
  
  dataPacket = struct('path','/foo','tt','f','data',{num2cell([1.0])}); % create packet
  osc_send(oscAddress, dataPacket); % write packet to OSC
  
  oscMessage = osc_recv(oscServer, 0.1); % listen for packet on OSC
  
  % check to see if anything is there...
  if length(oscMessage) &gt; 0
      fprintf('Found something!')
  else
      fprintf('Failed to find anything')
  end
  
  osc_free_address(oscAddress);
  osc_free_server(oscServer);
  </code></pre>
  
  <p>If I send using host 'localhost', everything works fine sending <em>from</em> MATLAB <em>to</em> MATLAB using the code above. If I set it to '127.0.0.1', MATLAB sends to Pure Data, but MATLAB then can't receive its own messages.</p>
  
  <p>Now for the other end of things. Here's my Pure Data patch:</p>
  
  <p><img src="https://i.stack.imgur.com/aGjUv.png" alt="Pure Data patch image"></p>
  
  <p>Again, running the above patch alone successfully sends and receives messages through Pure Data.</p>
  
  <p>The problem lies when I try to talk from one program to another. If I set things so that MATLAB is sending on port 3333 and Pure Data is receiving on 3333, and that Pure Data is sending on 2222 and MATLAB is receiving on 2222, I can make Pure Data <em>receive</em> if MATLAB's host is '127.0.0.1'. But, with '127.0.0.1', MATLAB can't send to itself.</p>
  
  <p>In any case, no matter what I try, I'm unable to make Pure Data <em>send</em> to MATLAB, despite being able to get it to send to itself. I suspect it has something to do with the 'host' address.</p>
  
  <p>My actual IPv4 address (found using 'ipconfig' of the MS command prompt) is completely different from 127.0.0.1, and using the value specified here doesn't seem to make things work any better.</p>
  
  <p>I'm aware that I can't have more than one OSC server with the same port open at any one time and so my current attempt at a solution involves sending from MATLAB on one port, and sending from Pure Data on another, with only a single server open at one time on either port.</p>
  
  <p>Note I'm also aware that I use <code>/foo</code> for messages from MATLAB and <code>/test</code> from Pure Data. However, my MATLAB code indiscriminately receives everything sent over OSC, so this makes no difference.</p>
  
  <p>Any help getting PD to talk to MATLAB would be appreciated.</p>
  
  <p><em>Update</em>: Ive solved the 'localhost' issue and that doesn't seem to fix things (i had to add localhost to my Windows 'hosts' file). So, I may have been barking up the wrong tree by worrying about the localhost thing. But, I still can't get PD to talk to MATLAB.</p>
  
  <p><strong>Update #2</strong>: Amro has posted an elegant solution below and I <em>still</em> can't get MATLAB to receive messages from Pure Data. I've installed <a href="http://sourceforge.net/projects/closethedoor/" rel="noreferrer">CloseTheDoor</a> to monitor my UDP connections and notice that when MATLAB sets up a server, it uses the 'Interface' <code>[::0]</code>, whereas PD sets uses 'Interface' <code>0.0.0.0</code>. Since PureData is the one that successfully receives messages, perhaps I need to make MATLAB listen on <code>0.0.0.0</code> as well?</p>