问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/6506803/best-way-to-get-a-matlab-c-interface