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 comentarios

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.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 14 de Nov. de 2018
Editada: James Tursa el 15 de Nov. de 2018
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 comentarios

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.
James Tursa
James Tursa el 15 de Nov. de 2018
Yes, you can build C++ mex routines.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018b

Preguntada:

el 13 de Nov. de 2018

Editada:

el 15 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by