Need to find out errors in mex

1 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by