# Socket Communication with Matlab

**URL:** <https://forum.universal-robots.com/t/socket-communication-with-matlab/1699>\
**Category:** Robot Communication\
**Created:** [March 5, 2018, 10:21am UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699 "2018-03-05T10:21:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon89271420](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon89271420](https://forum.universal-robots.com/u/anon89271420)\
**Post date:** [March 5, 2018, 10:21am UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699/1 "2018-03-05T10:21:44Z")

</div>

Hi there,  
I try to implement to read data streams from the UR controller using the socket interface and port 30003. To do this I’m using Matlab. If I want to read the actual position in X for example I read Byte 444-451, like described in the Client\_Interface\_V3.5.xls and convert it to a floating point number. But the value makes no sense. Here is my example code:

socketClientRTC= tcpip(TCPclient, 30003, ‘NetworkRole’, ‘client’);  
fopen(socketClientRTC);  
data= socketClientRTC.fread;  
dataCartPosX= [uint8(data(444)) uint8(data(445)) …  
uint8(data(446)) uint8(data(447)) …  
uint8(data(448)) uint8(data(449)) …  
uint8(data(450)) uint8(data(451))];  
floatPosX= typecast(dataCartPosX, ‘double’);

Thanks in advance,  
Chris

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [March 6, 2018, 12:21pm UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699/2 "2018-03-06T12:21:50Z")

</div>

Hi Chris,

I don’t have Matlab to test your code but could it potentially be a byte order issue? Try swapbytes?

> **[Swap byte ordering - MATLAB swapbytes](https://www.mathworks.com/help/matlab/ref/swapbytes.html)**
>
> This MATLAB function swaps the byte ordering of each element in array X from little endian to big endian (or vice versa).

---

<div class="post-metadata">

**Author:** ![anon89271420](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon89271420](https://forum.universal-robots.com/u/anon89271420)\
**Post date:** [March 6, 2018, 1:03pm UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699/3 "2018-03-06T13:03:56Z")

</div>

Thanks for the answer.  
Yes I tried this also, but it doesnt work.

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [March 6, 2018, 3:07pm UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699/4 "2018-03-06T15:07:32Z")

</div>

Ok, does any of the rest of the message make sense? Like the message size or the time? Does the value change at all when you change the TCP X position? I do the same thing with the Python struct library and it seems to work.

---

<div class="post-metadata">

**Author:** ![anon89271420](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon89271420](https://forum.universal-robots.com/u/anon89271420)\
**Post date:** [March 12, 2018, 10:20am UTC](https://forum.universal-robots.com/t/socket-communication-with-matlab/1699/5 "2018-03-12T10:20:32Z")

</div>

Yes, it changes, but the values make no sense.
