F在tcpip连接中写入不同的以下fread?[matlab]

F在tcpip连接中写入不同的以下fread?[matlab],matlab,tcp,Matlab,Tcp,在编写一个类的过程中,将两个matlab实例连接在一起。这些实例将在不同的计算机上,但我目前正在一台计算机上进行测试 目前,我能够在两个Matlab之间建立连接,并且能够在它们之间发送/接收消息 代码: Matlab2 gh = connectcompstogether; gh.SetupClient(); gh.recmessage(); 发送的消息为8位双精度555。 然而,当查看接收到的消息时,结果是一个矩阵 64 129 88 我不明白发生了什么,因为我一直在跟踪的人没有这个问题 并

在编写一个类的过程中,将两个matlab实例连接在一起。这些实例将在不同的计算机上,但我目前正在一台计算机上进行测试

目前,我能够在两个Matlab之间建立连接,并且能够在它们之间发送/接收消息

代码:

Matlab2

gh = connectcompstogether;
gh.SetupClient();
gh.recmessage();
发送的消息为8位双精度555。 然而,当查看接收到的消息时,结果是一个矩阵

64
129
88
我不明白发生了什么,因为我一直在跟踪的人没有这个问题

并添加上下文。我正在尝试通过TCP-IP连接两个matlabs,以便可以控制一个实例与另一个实例。我的计划是让第二个matlab等待命令代码,并在第一个matlab请求时执行指定的函数。

tcpip/fread默认精度为uchar,因此默认情况下fread将输出一个8位无符号整数的列数组

您需要指定预期的双精度:

%size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');
或将uint8数组类型转换为双精度:

rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
% cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
% then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
% a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));

谢谢你的解释。这就解释了为什么我会收到一个数组。给出的第一个例子解决了这个问题。但我尝试了第二种方式,打字似乎对消息没有影响。有什么原因吗?也许Matlab将输入读取为字节,然后将其强制转换为双精度(即,输出一个具有0-255范围内整数值的双精度浮点数组)。因此,
rawMessage
应手动转换为uint8。我将编辑答案。
%size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');
rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
% cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
% then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
% a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));