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!

4

1 回答 1

0

查看此页面上的信息http://www.mathworks.co.uk/help/techdoc/ref/fread.html似乎当您指定它时,将期望 64 位数据读入每个'int64'fread. 因此,您的 4 个元素被读取为单个 64 位模式。我认为,如果您要发送 16 位短整数,则需要使用fread与发送相同的数据类型'uint16',然后将它们“转换”为 64 位。没有转换,例如无符号短裤的范围在 64 位整数范围内。如果你真的想要按照自己的方式进行操作并读入 64 位整数值,我认为您需要将数据作为 64 位整数数组发送到套接字发送函数中。这样至少 Matlab 将从另一端的套接字读取正确的字节数。目前,从外观上看,您发送的每 4 个无符号短值都会合并为一个 64 位整数。

于 2012-07-24T08:14:19.323 回答