Can MATLAB use a MEX Function that run continuously?

I have a socketing program writtne in C++ which runs continously and I'm trying to use MATLAB to plot the data. I looked into MEX functions but all the examples are of functions that execute and return a single value. Is there a way to continuously feed data from C++ into MATLAB and produce live graphs?

3 Comments

perhaps invert the flow?
https://www.mathworks.com/help/matlab/calling-matlab-engine-from-c-programs-1.html
I thought about just using the MATLAB enginge in my C++ program but I have some pretty complex MATLAB code that won't be easy to call in C++.
MATLAB uses a single main execution thread unless you use the Parallel Computing Toolbox. When you call into .mex then nothing in the main MATLAB engine can get done, including not processing any graphics that has not been handed over to the graphics thread already. Even timers cannot execute.
So if you infinite loop in mex doing data collection, then you cannot process the data on the MATLAB side.
It is valid to collect multiple values before returning, and there is always a trade off between waiting collecting values efficiently and returning for more processing, against function call overhead every time you go back for more data: the less data you collect the faster you can react to each datum but the more overhead in the collecting.

Sign in to comment.

 Accepted Answer

If you are willing to do everything from within the mex routine, you could gather your data iteratively in a loop and then within that loop call back to MATLAB to do the plotting. E.g., a simple example:
/* Simple mex routine to animate a sine wave. Takes no input arguments */
#include "mex.h"
#include <math.h>
#define PI 3.14159265358979
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *mx, *my, *mon;
mxArray *rxy[2], *ron[1];
double *x, *y;
int i, j, N = 1000;
mx = mxCreateDoubleMatrix(1,N,mxREAL);
my = mxCreateDoubleMatrix(1,N,mxREAL);
mon = mxCreateString("on");
rxy[0] = mx;
rxy[1] = my;
ron[0] = mon;
x = mxGetPr(mx);
y = mxGetPr(my);
for( i=0; i<N; i++ ) {
x[i] = i * 4 * PI / N;
}
for( j=0; j<1000; j++ ) {
for( i=0; i<N; i++ ) { /* generate the data for this iteration */
y[i] = sin(x[i] + j*PI/100);
}
mexCallMATLAB(0,NULL,2,rxy,"plot");
mexCallMATLAB(0,NULL,1,ron,"grid");
mexCallMATLAB(0,NULL,0,NULL,"drawnow");
}
mxDestroyArray(mon);
mxDestroyArray(my);
mxDestroyArray(mx);
}
In the example above it simply runs the animation in a loop with a definite end. In your case you may want to use a forever loop and then have something within the loop trigger an exit.

2 Comments

This is actually a great idea. This would force me to move all the computation into C, correct? Can you still make a MEX function if you use C++? I feel like the math might be easier if I could use some C++ libraries.
Yes, you can build C++ mex routines.

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!