필터 지우기
필터 지우기

Need to find out errors in mex

조회 수: 2 (최근 30일)
Abeera Tariq
Abeera Tariq 2015년 5월 30일
편집: Abeera Tariq 2015년 6월 2일
I am trying to make file of this code. mex file is created but when i try to use this mex file my matlab crashes can someone locate the error?
float **sum;
sum= (float *)mxMalloc(sizeof(float)*q*f2);
I am feeling like i am hitting my head against the wall.. Pleaseeee help
  댓글 수: 3
Jan
Jan 2015년 5월 30일
편집: Jan 2015년 5월 31일
Please provide some inputs such that we have at least the chance to run your code. Without useful comments it is hard enough to read the code and guess if it does, what you want.
In which line does the crash appear? Just insert some "return" statements to locate the problem.
Abeera Tariq
Abeera Tariq 2015년 5월 31일
I am using
>> X=ones(20,4);
>> X=single(X);
>> I=ones(9,8);
>> prp(X,1,1,1,1,1,I);
Prp is the mex file created by this code

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

채택된 답변

James Tursa
James Tursa 2015년 5월 31일
편집: James Tursa 2015년 6월 1일
Remember the advice I gave you about abandoning your desire to maintain the multi-level [ ][ ] indexing scheme when using dynamic memory? Your posted code is a perfect example of why I gave that advice. I will focus on only one of the variables to show the problem:
double **idx1;
:
idx1= (double *)mxMalloc(sizeof(double)*rmax*cmax);
:
for (i = 0; i < rmax; i++){
for( j = 0 ; j <cmax ; j++ ){
idx1[i][j]= I[i][j]; }}
idx1 is of type "pointer to pointer to double". That is, it points to memory that is supposed to contain "pointer to double"s. Your allocation is all wrong. It should be something like this:
idx1= (double **)mxMalloc(sizeof(double *) * SOMETHING);
Then you can fill idx1 with other pointers that point to the actual double data. What you CAN'T do is use the [ ][ ] indexing with it until you have first filled in the pointer values. I.e., this line will certainly crash MATLAB since idx1[i] is an uninitialized value (contains an invalid pointer):
idx1[i][j]= I[i][j];
I don't have time at the moment to write up a proper way to do this in C, but will try to do so this evening.
EDIT: 6/1/2015
OK, here is a short example showing how to allocate a 2D matrix dynamically (i.e. you don't know the sizes ahead of time so the sizes are not constant) and still use the [ ][ ] indexing syntax. The keys are that each level of [ ] requires a separate allocation, and that you need to fill in the row pointers manually in order to get the [ ][ ] syntax to work.
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double **x;
double *d;
mwSize i, j, m, n;
m = 3;
n = 4;
d = (double *) mxMalloc( m * n * sizeof(*d) ); // Allocate the full matrix
x = (double **) mxMalloc( m * sizeof(*x) ); // Allocate the row pointers
for( i=0; i<m; i++ ) {
x[i] = d + i*n; // Set the row pointers to point inside the full matrix
}
for( i=0; i<m; i++ ) {
for( j=0; j<n; j++ ) {
x[i][j] = 10*(i+1) + (j+1); // Set the double data
mexPrintf("x[%d][%d] = %f\n",i,j,x[i][j]); // print it out
}
}
mxFree(x); // Free the row pointers
mxFree(d); // Free the double data
}
So you can now see the extra effort involved in getting this to work properly, and the only benefit is so that you can use the [ ][ ] syntax. And if you have a 3D array and want to use [ ][ ][ ] indexing, then you will need another level of allocation and another loop to manually fill in the pointers for this additional array.
  댓글 수: 1
Abeera Tariq
Abeera Tariq 2015년 6월 2일
Thanks a lot James. You are just Awesome..

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by