Mex file. error in passing data.
Afficher commentaires plus anciens
Hello,
i am struggling with my very first Mex file experience.
I wrote a mex function as follows.
file name is 'hello_world.c'
#include <math.h>
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
double *C, *Node_val;
int l1,l2, v1, v2, k, n, K,ip,jp,j,i1,i2;
double Cijk;
double *CE,*Hash;
// getting rhs pointers for scalars to be used as input
l1 = mxGetScalar(prhs[0]);
l2 = mxGetScalar(prhs[1]);
v1 = mxGetScalar(prhs[2]);
k = mxGetScalar(prhs[3]);
K = mxGetScalar(prhs[4]);
n = mxGetScalar(prhs[5]);
Hash=mxGetPr(prhs[6]);
C=mxGetPr(prhs[7]);
Node_val=mxGetPr(prhs[8]);
//printf("H: %f",Hash);
// create a pointer for output
plhs[0] = mxCreateDoubleMatrix(n, k, mxREAL);
CE = mxGetPr(plhs[0]);
for (v2 = l1-1 ; v2 <=l2-1; v2=v2+1)
{
j=Hash[v2];
if (j <=2 || j >= K-1)
{ i1 = Hash[v2+n];
i2 = Hash[v2+2*n];
ip = 1/2*(j-i1);
jp = 1/2*(j+i1);
Cijk = C[ip+(jp)*(K/2+1)];
ip = 1/2*(j-i2);
jp = 1/2*(j+i2);
CE[v2] = Cijk+C[ip+(jp)*(K/2+1)]+Node_val[v1-1];
printf("CE: %f",CE);
}
}
}
This Matlab code i am calling this mexfunction from is following. I am getting erroneous output as CE[1:3]=[0,8.0362, 8.036, 8.036,........0,0]. I understand that the Hash[v2] is not passing integer to j as j is int and Hash is a double pointer.
Hash=rand(14,3);
n=14;
K=4;
k=1;
l1=2;
l2=4;
v1=1;
C=[4.01808033751942,1.23318934835166,4.17267069084370;0.759666916908419,1.83907788282417,0.496544303257421;2.39916153553658,2.39952525664903,9.02716109915281];
Node_val=ones(n,3);
Node_val(1,:)=[0 0 0];
%CE=ones(n,1);
mex hello_world.c
CE=hello_world(l1,l2,v1,k,K,n,Hash,C,Node_val);
1 commentaire
amit jha
le 17 Mai 2020
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!