Matlab shuts down after executing a mex call

Hi,
I'm calling a C++ function using Mex. The function call works fine. After the file is called and the result is displayed, Matlab shuts down by itself. How do I stop this from happening?

 Respuesta aceptada

James Tursa
James Tursa el 5 de Mzo. de 2012
plhs[] is an array that MATLAB creates when you call the mex function to hold the return variables. But MATLAB doesn't create these variables for you, it simply creates a pointer array to hold the pointers to the return variables. You the programmer are expected to create these variables. When you enter the mex function, plhs[] values are not yet pointing to any variables. When you do this:
x = mxGetPr(plhs[0]);
You are asking mxGetPr to get the real data pointer from the variable pointed to by plhs[0], but plhs[0] is an uninitialized pointer, hence the crash. You need to do this:
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); // create the return variable
x = mxGetPr(plhs[0]);
And, as Walter points out, even if you do this as you currently have the code you will get a 0 returned value since you don't assign anything to the output yet via the x pointer. E.g., you could change the last line to this:
*x = test(a,b);

2 comentarios

Jan
Jan el 5 de Mzo. de 2012
In addition, nrhs is read only. You can check it to see how many values are expected from the caller. But you cannot set it inside the Mex function.
Vimal
Vimal el 5 de Mzo. de 2012
Thanks James.
That clears it up.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 4 de Mzo. de 2012

0 votos

You probably have some memory corruption in the C++ function, with the effect of the corruption being observed when MATLAB goes to clear the variables that are no longer being used.

4 comentarios

Vimal
Vimal el 5 de Mzo. de 2012
I'm testing mex functionality to see if it will work for something else I need.
I'm just passing 2 variables from matlab to the C++ function which adds them up and prints them.
Walter Roberson
Walter Roberson el 5 de Mzo. de 2012
Try posting code. James is pretty good at spotting mex problems if he has code to work with.
Vimal
Vimal el 5 de Mzo. de 2012
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <complex>
using namespace std;
#include "mex.h"
int test(int a, int b)
{
int s;
s=a+b;
cout<<s;
return s;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int a, b, s;
double *x;
nrhs=2;
a=mxGetScalar(prhs[0]);
b=mxGetScalar(prhs[1]);
x=mxGetPr(plhs[0]);
test(a,b);
}
Vimal
Vimal el 5 de Mzo. de 2012
I call this by entering the following lines:
mex test.cpp;
a=1;
b=2;
sum=test(a,b)

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 5 de Mzo. de 2012

0 votos

What do you do with the value you return from test() at the C level ? You are not somehow putting it in the the output location x (plhs[0]) in your mexFunction routine .

2 comentarios

Vimal
Vimal el 5 de Mzo. de 2012
I'm not doing anything with it.
test() could be a function that returns void.
Walter Roberson
Walter Roberson el 5 de Mzo. de 2012
True that test() at your C level could return void, but at the MATLAB level you have indicated that you are expecting a return value from the call to the MEX routine, and you never set the return value to anything.

Iniciar sesión para comentar.

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by