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?

 채택된 답변

James Tursa
James Tursa 2012년 3월 5일

2 개 추천

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

Jan
Jan 2012년 3월 5일
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 2012년 3월 5일
Thanks James.
That clears it up.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 3월 4일

0 개 추천

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

Vimal
Vimal 2012년 3월 5일
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.
Try posting code. James is pretty good at spotting mex problems if he has code to work with.
Vimal
Vimal 2012년 3월 5일
#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 2012년 3월 5일
I call this by entering the following lines:
mex test.cpp;
a=1;
b=2;
sum=test(a,b)

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2012년 3월 5일

0 개 추천

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

Vimal
Vimal 2012년 3월 5일
I'm not doing anything with it.
test() could be a function that returns void.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

질문:

2012년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by