10

I have a C++ Windows Program and I want to convert and visualize some data from this C++ app in an existing Matlab Program.

Currently I am writing the data from the C++ app into files. At the same time the Matlab app reads the files and processes the data. (polling) It basically works but I am running in performance troubles when the data load gets to high.

What is the best solution to transfer data between this programs? I am thinking of a kind of message queue or socket interface.

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
fpdragon
  • 1,867
  • 4
  • 25
  • 36
  • Perhaps this link could help? [Using Matlab as a plotting library for C++?](http://www.mathworks.de/matlabcentral/newsreader/view_thread/81552) and this: [Calling MATLAB Engine from C/C++ and Fortran Programs](http://www.mathworks.com/help/techdoc/matlab_external/f38569.html) And about socket programming: [socket programming in MATLAB ?](http://stackoverflow.com/questions/3164348/socket-programming-in-matlab) There are also files int the Matlab file exchange that might help you: [TCP/IP Socket Communications in MATLAB](http://www.mathworks.com/matlabcentral/fileexchange/21131) Although I think – zw324 Jun 28 '11 at 13:23

2 Answers2

5

Use the Matlab API to send your data from C++ to Matlab, then execute a plot command on it. Roughly, do the following -- there are no error checks, but the gist is there:

#include <engine.h>
//open the engine
Engine *m_engine;
m_engine = engOpen("\0");

//put our data
//pretend this is a 2 column, n row matrix, so we can do a 2D plot
mxArray* mx = mxCreateDoubleMatrix(mat->n_rows, mat->n_cols, mxREAL);
memcpy(mxGetPr(mx),some_data,data->n_elem*sizeof(double));
put("data",mx);
mxDestroyArray(mx);

//plot
engEvalString(m_engine, "plot(data(:,1),data(:,2),'-o')");

Just remember, Matlab works in column major, while C++ is row major.

Chris
  • 1,532
  • 10
  • 19
0

The best way is to use the MATLAB engine from C/C++ code. All you have to do is to invoke the MATLAB engine from C/C++ program and then you can easily execute MATLAB commands directly from the C/C++ program.

Please take care that you will have to include additional library files of MATLAB into the project, for the same to work. You can have a look at a working example for the same as shown here.