Reputation: 264
I have a very large data structure in some Matlab code that is in the form of cells of arrays. We want to develop C code to work on this data, but I need some way to store the Matlab variable (which we generate in Matlab) and open it in a C/C++ program. What is the easiest way to bridge the two programs so I can transfer the data?
Upvotes: 4
Views: 1867
Reputation: 614
If you are only moving the data from MATLAB to C occassionally, the easiest thing would be to write it to a binary file, then read from the file in C. This of course leaves the C code completely independent of MATLAB.
This does not have to be that messy if your data structure is just a cell array of regular arrays, e.g.
a{1} = zeros(1,5);
a{2} = zeros(1,4);
You could just write a header for each cell, followed by the data to the file. In the above case, that would be:
[length{1} data{1} length{2} data{2}]
In the above case:
5 0 0 0 0 0 4 0 0 0 0
If the arrays are 2D, you can extend this by writing: row, column, then the data in row-major order for each cell.
This might not be entirely convenient, but it should be simple enough. You could also save it as a .mat file and read that, but I would not recommend that. It is much easier to put it in a binary format in MATLAB.
If you need to move the data more frequently than is convenient for a file, there are other options, but all I can think of are tied to MATLAB in some way.
Upvotes: 1
Reputation: 20915
If the two processes need to connect during their lifecycle, you have plenty of options:
If the communication is offline (After Matlab closes, C++ starts to read), then you should use filesystem. Try to format it in XML, it is a well recognized standard.
Upvotes: 0
Reputation: 16035
You should use mex files:
http://www.mathworks.fr/support/tech-notes/1600/1605.html
Upvotes: 0