0

I want to send unsigned short vector in visual c++ to matlab using socket communication.

I have an unsigned short vector std::vector test that has

//test[0]-test[3]  short values comverted from longlong (64bits/16bits=4 elements)    
//test[4]-         short values.    

Now I want to socket this vector to matlab.

In VC++,

send(socket, (char *) &test[0], test.size()* sizeof(unsigned short), 0);  

In matlab m-file, I tried

//t=tcpip(----);    
//fread(t, 4, 'uint64');    
//fread(t, the rest of bytes, 'ushort');    

but values I got on matlab looks wrong.

"fread(t, 4, 'uint64')" shows invalid precision and
"uint64(fread(t,4,'uint64'))" does also the same error.

How can we receive and convert data on matlab?

Thanks in advance!

H.Muster
  • 9,297
  • 1
  • 35
  • 46

1 Answers1

0

Looking at the information on this page http://www.mathworks.co.uk/help/techdoc/ref/fread.html it appears that when you specify 'int64' into fread it is going to be expecting 64 bits of data to read into each value. Therefore your 4 elements are are read as a single 64 bit pattern. I think if you are sending 16 bit short integers, you will need to use fread with the same data type as you send 'uint16' and then 'convert' them to 64 bits. There is no conversion as such as the range of unsigned shorts is way within that of 64 bit integers. If you really want to do it your way and read into 64 bit integer values, I think you will need to send your data as an array of 64 bit integers into the socket send function. This way at least Matlab will be reading the correct number of bytes from the socket at the other end. At the moment each 4 unsigned short values you send get merged into one single 64 bit integer by the looks of it.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101