mexfunction out of memory
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I wrote this code in mexfunction but get the error in compilation.
#include "mex.h"
#include "math.h"
#define source(xo,yo) source[xo+yo*wo]
#define target(xi,yi) target[xi+yi*wi]
#define A(xi,yi,xo,yo) A[xi+ yi*9+ xo*9*9+ yo*9*9*9]
/* The computational routine */
void CrossActivity(double *A , double *source, double *target, int kg,int wi,int hi, int wo, int ho)
{
int xi,yi,xo,yo;
float d;
for ( xi = 0; xi <wi; xi++) {
for ( yi = 0; yi < hi; yi++) {
for ( xo = 0; xo < wo; xo++) {
for ( yo = 0; yo < ho; yo++) {
d = (source(xo,yo) - target(xi,yi))/ 255.0;
A(xi,yi,xo,yo) = exp((-kg) * (d * d));
}
}
}
}
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
//mwSize wi;
const double *source; /* output matrix */
const double *target;
double *output;
/* create a pointer to the real data in the input matrix */
int kg=(int)mxGetPr(prhs[2]);
int wi=(int)mxGetPr(prhs[3]);
int hi=(int)mxGetPr(prhs[4]);
int wo=(int)mxGetPr(prhs[5]);
int ho=(int)mxGetPr(prhs[6]);
/* create the output matrix */
//const mwSize dims[]={9,9,9,9};
mwSize dims[]={wi,hi,wo,ho};
plhs[0]=mxCreateNumericArray(4,dims, mxDOUBLE_CLASS, mxREAL);
output = mxGetPr(plhs[0]);
//output= mxMalloc(sizeof(double)*24*24*24*24);
source = mxGetPr(prhs[0]);
target = mxGetPr(prhs[1]);
/* call the computational routine */
CrossActivity(output,source,target,kg,wi,hi,wo,ho);
return;
}
0 Kommentare
Antworten (2)
Steven Lord
am 26 Jun. 2015
Your compiler should have yelled at you, I think, for your C-style cast of mxGetPr's output into an int. Look at what mxGetPr is documented to return. You probably want to use mxGetData or perhaps mxGetScalar instead, though you will need to check the class of the input to determine to what type you should cast the output.
If you received a different error from your compiler, post the error message.
0 Kommentare
James Tursa
am 26 Jun. 2015
Bearbeitet: James Tursa
am 26 Jun. 2015
You should get in the habit of using parentheses in all your macro "functions" to avoid nasty bugs in future code. E.g., these lines:
#define source(xo,yo) source[xo+yo*wo]
#define target(xi,yi) target[xi+yi*wi]
#define A(xi,yi,xo,yo) A[xi+ yi*9+ xo*9*9+ yo*9*9*9]
should be written this way:
#define source(xo,yo) source[(xo)+(yo)*(wo)]
#define target(xi,yi) target[(xi)+(yi)*(wi)]
#define A(xi,yi,xo,yo) A[(xi)+ (yi)*9+ (xo)*9*9+ (yo)*9*9*9]
And I agree with Steven, that if you want to get the values out of mxArray variables you should be using something like mxGetScalar instead. E.g.,
int kg = (int)mxGetScalar(prhs[2]);
int wi = (int)mxGetScalar(prhs[3]);
int hi = (int)mxGetScalar(prhs[4]);
int wo = (int)mxGetScalar(prhs[5]);
int ho = (int)mxGetScalar(prhs[6]);
Finally, your code is not robust against user input errors. You should check all input variable for quantity, correct type and dimensions, etc.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!