Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Geting pointer to class object in mex file

Subject: Geting pointer to class object in mex file

From: Amit

Date: 12 Jul, 2011 19:54:10

Message: 1 of 5

Hi,

I want to use the pointer to class properties in mex file. I am trying to use the function GetPropertyPtrx.c after compiling it, but i am getting error saying mismatch of pointer type. Please suggest me where is the problem. details of the problem is as below.

Thanks,
Amit

I have simple matlab class like this-

  classdef amitdata
    properties
        pathnum;
        path
        indices
    end %properties

I have a object named mydata like this-
mydata(1:N)=amitdata; % N is input to code

then I store unit16 data in the peropeties path and indices and double in pathnum. Now I want to use these three properties in mex file to find a scalar Z-

#include "matrix.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
int iter,i, j, m, n, r, q, a, b;
unsigned short *pth, *indi;
double *pnum;
double Z, mincost;
/* get the pointer to the properties */

for(iter=0;iter<N;iter++)
{
  pnum=GetPropertyPtrx(mydata(1).pnum);
  pth=GetPropertyPtrx(mydata(1).path);
  indi=GetPropertyPtrx(mydata(1).indices);

for (i=0;i<r;i++)
            {
                 a=indi[i]-1;
                 b=indi[i+1]-1;
                 cost=0;
                for(j=a;j<b;j++)
                {
                   q=paths[j]-1;
                   cost+=linkcost[q];
                }
                  pathcost[i]=cost;
             /*tracks the minimum of pathcost */
                 if(mincost>cost){mincost=cost}
            }
/* Z is found as sum of diffrence of pathcost and mimimum of pathcost*/
 
}
 return;
}


linkcost is a vector and is input to mex function.
mexfunction is saved as amit_mex_fun.c and compling by mex command is called as-

Z=amit_mex_fun(mydata,N,linkcost);
  

Subject: Geting pointer to class object in mex file

From: Amit

Date: 12 Jul, 2011 20:01:09

Message: 2 of 5


I used the function GetPropertyPtrx.c from matlab central file exchange. Link for the same is-

http://www.mathworks.com/matlabcentral/fileexchange/30672-mxgetpropertyptr-c-mex-function/content/GetPropertyPtrx.m

Amit

Subject: Geting pointer to class object in mex file

From: James Tursa

Date: 13 Jul, 2011 00:26:10

Message: 3 of 5

"Amit " <amitsingh.singh@gmail.com> wrote in message <ivi8p2$j2i$1@newscl01ah.mathworks.com>...
> Hi,
>
> I want to use the pointer to class properties in mex file. I am trying to use the function GetPropertyPtrx.c after compiling it, but i am getting error saying mismatch of pointer type. Please suggest me where is the problem. details of the problem is as below.
>
> Thanks,
> Amit
>
> I have simple matlab class like this-
>
> classdef amitdata
> properties
> pathnum;
> path
> indices
> end %properties
>
> I have a object named mydata like this-
> mydata(1:N)=amitdata; % N is input to code
>
> then I store unit16 data in the peropeties path and indices and double in pathnum. Now I want to use these three properties in mex file to find a scalar Z-
>
> #include "matrix.h"
> #include "mex.h"
> void mexFunction(int nlhs, mxArray *plhs[],
> int nrhs, const mxArray *prhs[])
> {
> int iter,i, j, m, n, r, q, a, b;
> unsigned short *pth, *indi;
> double *pnum;
> double Z, mincost;
> /* get the pointer to the properties */
>
> for(iter=0;iter<N;iter++)
> {
> pnum=GetPropertyPtrx(mydata(1).pnum);
> pth=GetPropertyPtrx(mydata(1).path);
> indi=GetPropertyPtrx(mydata(1).indices);
>
> for (i=0;i<r;i++)
> {
> a=indi[i]-1;
> b=indi[i+1]-1;
> cost=0;
> for(j=a;j<b;j++)
> {
> q=paths[j]-1;
> cost+=linkcost[q];
> }
> pathcost[i]=cost;
> /*tracks the minimum of pathcost */
> if(mincost>cost){mincost=cost}
> }
> /* Z is found as sum of diffrence of pathcost and mimimum of pathcost*/
>
> }
> return;
> }
>
>
> linkcost is a vector and is input to mex function.
> mexfunction is saved as amit_mex_fun.c and compling by mex command is called as-
>
> Z=amit_mex_fun(mydata,N,linkcost);
>

I don't have time for a detailed response at the moment, so I will post more later. But basically you are mis-using the FEX code. The only thing you need to do in your code is this:

#include "mxGetPropertyPtr.c"
     :
mxArray *pathnum, *path, *indices;
     :
pathnum = mxGetPropertyPtr(prhs[0],0,"pathnum");
path = mxGetPropertyPtr(prhs[0],0,"path");
indices = mxGetPropertyPtr(prhs[0],0,"indices");

That will get the pointers to the properties of the mydata(1). Then you can use mxGetPr(pathnum) or mxGetData(pathnum) etc to get at the contents of the properties.

James Tursa

Subject: Geting pointer to class object in mex file

From: Amit

Date: 13 Jul, 2011 18:43:28

Message: 4 of 5

 
> I don't have time for a detailed response at the moment, so I will post more later. But basically you are mis-using the FEX code. The only thing you need to do in your code is this:
>
> #include "mxGetPropertyPtr.c"
> :
> mxArray *pathnum, *path, *indices;
> :
> pathnum = mxGetPropertyPtr(prhs[0],0,"pathnum");
> path = mxGetPropertyPtr(prhs[0],0,"path");
> indices = mxGetPropertyPtr(prhs[0],0,"indices");
>
> That will get the pointers to the properties of the mydata(1). Then you can use mxGetPr(pathnum) or mxGetData(pathnum) etc to get at the contents of the properties.
>
> James Tursa

Thanks James for the reply. I need further help to resolve the problem in my code. I tried the modifications suggested by you , I compiled it but it is crashing saying segmentation violation when I call it like below-
 z=find_ngap(mydata,N,linkcost);

I have script file as below to generate test data set-

N=10;
mydata(1:N)=amitdata;
A=uint16(1:10);
B=uint16([1,3,6]);
pathcost=11:20;
for iter=1:1:N
  mydata(iter).pathnum=3;
  mydata(iter).paths=A;
  mydata(iter).indices=B;
end

After running above script file, I compiled it sucussfully (using mex -largeArrayDims find_ngap.c) and then call the mex file like this from the command window-
z=find_ngap(mydata,N,linkcost);

The name of my source code for mex file is find_ngap.c and is written as below-

#include "mxGetPropertyPtr.c"
#include "matrix.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{

 /* varaible declaration */
   mxArray *pathnum_ptr, *paths_ptr, *indices_ptr, *pathcost_ptr;
   //mwIndex i;
   int N, i, j, m, n, r, q, a, b, iter;
   int *N_ptr;
   unsigned short pp;
   unsigned short *paths, *indices; /*pointer variables to store uint16*/
   double *linkcost, *pathnum, *pathcost, *put;
   double cost, excesscost, mincost=9999.0;
   
  /* Create matrix for the input and return argument. */
            plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
            put=mxGetPr(plhs[0]);
   /*get inputs*/
       N_ptr=mxGetPr(prhs[1]);
       N=N_ptr[0];
       linkcost=mxGetPr(prhs[2]);
        
  /* Finding excesscost */
         excesscost=0;
  for(iter=0;iter<N;iter++)
  {
       pathnum_ptr = mxGetPropertyPtr(prhs[0],iter,"pathnum");
       paths_ptr = mxGetPropertyPtr(prhs[0],iter,"paths");
       indices_ptr = mxGetPropertyPtr(prhs[0],iter,"indices");
        pathnum=mxGetPr(pathnum_ptr);
        paths=mxGetData(paths_ptr);
        indices=mxGetData(indices_ptr);
        n=pathnum[0];
        pathcost_ptr=mxCreateDoubleMatrix(1,n, mxREAL);
        pathcost=mxGetPr(pathcost_ptr);
        r=n-1;
     for (i=0;i<r;i++)
     {
         a=indices[i]-1;
         b=indices[i+1]-1;
         cost=0;
        for(j=a;j<b;j++)
        {
           q=paths[j]-1;
           cost+=linkcost[q];
        }
          pathcost[i]=cost;
       /*track the minimum of pathcost */
         if(mincost>cost){mincost=cost;}
     }
     for (i=0;i<r;i++)
     {
       excesscost+=pathcost[i]-mincost;
     }
  }
    
      put[0]=excesscost;
      
}

Sorry for such a long post. But I thought it is difficult to find the bug without giving details.

Amit

Subject: Geting pointer to class object in mex file

From: James Tursa

Date: 14 Jul, 2011 07:04:14

Message: 5 of 5

"Amit " <amitsingh.singh@gmail.com> wrote in message <ivkp0g$pqm$1@newscl01ah.mathworks.com>...
>
> N=10;

At the MATLAB level variable N is a double.

> z=find_ngap(mydata,N,linkcost);

It gets put in as the 2nd argument in the find_ngap call.

> int *N_ptr;

Inside your mex routine you declare N_ptr as pointing to an int.

> N_ptr=mxGetPr(prhs[1]);

Then you point it to the data for the 2nd argument, which is a double and not an int32 type.

> N=N_ptr[0];

Then you dereference the 8-byte double data as a 4-byte int and get garbage. So try changing the declaration of N_ptr to this:

    double *N_ptr;

I haven't looked at the rest of your code but try fixing this first.


James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us