MATLAB Answers

Convert C struct to matlab struct

40 views (last 30 days)
zohar on 24 Aug 2011
Hi all
I have two applications the first is written in c language and the other in matlab language, they are both communicateby using tcp.
Application 1(c language) send struct data to application 2(matlab language).
How can i make matlab recognize the received data as a struct data?
For example :
C code
struct My_struct1
int x;
int y[10];
struct My_struct2
char x1;
char y1[10];
float x2;
float y2[10];
double x3;
double y3[10];
My_struct1 z;
I am sending My_struct2 from c to matlab.
This is the matlab code
Objtcp = tcpip('');
set(Objtcp, 'RemotePort', 55555)
if (get(Objtcp, 'BytesAvailable') > 0)
[Receivedata len] = fread(Objtcp, Objtcp.BytesAvailable);
% Now the Receivedata is 'double' type
I want to convert/cast the Receivedata that will be My_struct2 type


Sign in to comment.

Answers (1)

Pierre on 24 Aug 2011
I never did that in particular, but I personally consider there are three approaches:
1. Receive the byte-stream as is, but then you have to know exactly how your data is encoded at the sender (what's the size of the types, which order are the member-variables held in memory, are they Big Endian or Little Endian, etc.) and convert it manually back into a matlab struct. But IMHO this is going to be quite a pain and somewhat errorprone.
2. Familiarize yourself with Serialization and Marshalling of data, allowing you for example to convert your struct data into XML, YAML, or Struxt format, transmit the converted data (containing the struct's structural information in addition to the data contained by it) and deserialize it on the Matlab side. (There should be c libraries around for supporting serialization tasks. Not quite sure about libraries for MATLAB, but in the end an XML-reader will do the task on MATLAB end.)
3. Familiarize yourself with Serialization, Marshalling, and the SOAP capabilities of MATLAB.
I would probably go for option 2 because it should be a reasonable balance between "the way things should be done" and "just get your prototype running".


zohar on 24 Aug 2011
Hi Pierre
Thank you for your response!
1. I know how to get small struct (convert it manually ) and yea it's pain :-(.
2. I am working with c/c++ and the struct I am dealling with is very big, and have lot of sub-structs, so XML is possible but again I should do it manually (creating the XML). XML suggestion is very good when working with c#.
3. I don't know what is SOAP , but as wiki said it's based on XML.
Any help
Pierre on 26 Aug 2011
I understand your opinion about 2. but I do not completely agree with you:
It is true that the .Net framework supports XML "out-of-the-box" and that it is relatively simple to do tasks as Serialization/Deserialization in contrast to C++, but this is not related to the XML-part, it is related to the built-in capabilities of the programming language. C# provides by design a mechanism called reflection which aggressively simplifies the task of (de-)serialization. Reflection is not buit-in in C++ (there are libraries providing similar technology--e.g. QT, with QMeta/QMetaData--but extending your code to support this would be more effort than gain for you I guess).
On your concern about XML support in C++, there are XML-reader/writer libraries available like xml++ or tinyxml, which definitely simplify the generation of the XML streams and take care of correct conversion of basic types. But it is true that you have to write a minimum of code to tell your programm what to convert in which way, and I'm afraid there is just no way around this issue...
There is also Boost.Serialization which is intended to simplify the task of serialization, but using this library requires already serious skills in development and architectural design.
About 3: It uses XML for communication but is not based on it ;) SOAP is a network protocol in order to realize (object-enabled) Remote Procedure Calls (and by consequence has object-transfer capabilities... made available through the use of XML). But that's the way to go if you want to sell your code to customers as productive code, if you aren't you should drop the SOAP part and stick to 2 (which you had to acquire in anyway to succeed in 3 ;) ).
There are also code synthesizers for which you declare the class structures and properties you'd like to serialize. Then a generator generates class definitions and implementations of the serialization part of the classes. (E.g. XSD/e) But this requires again writing the definition files (in a language you'd probably to learn at first).
I'm really sorry, there is just no magic solution for this. The elegant solutions are--AFAIK--not so simple to get started with... if you need it regularly for new projects, it might be worth the effort to learn using one of those "elegant" libraries, but I'm not quite sure if it's worth for once! :S
Pierre on 26 Aug 2011
... and there we only talked about the C++ side.

Sign in to comment.

Sign in to answer this question.