MATLAB: Sending data via TCP from Matlab to c

Instrument Control Toolboxtcp

Hi all I am trying to send data from matlab to c. In order to do so I am using the function fwrite.
My question is : how to send it as an struct ? for example
Msg.Size = int32(8);
Msg.Data = '12345678';
t = tcpip('10.1.4.34');
set(t, 'RemotePort', 55555)
set(t, 'InputBufferSize', 30000)
fopen(t)
It works OK When I execute the following
fwrite(t,Msg.Size,'int32');
fwrite(t,Msg.Data,'char');
How can I send the whole struct Msg ?
Any help will be appreciated.

Best Answer

Instead of writing the data directly to tcp, save it in a uint8 variable (use typecast() as necessary.) Once you have put together a block to send, the size of the uint8 variable is the message length and the uint8 array is the data. (Sometimes the message length has to include the size of the bytes used to represent the length.)
If you are working on the same system, you may find using memory mapping to be easier than tcp. See here