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:
double to long int type change in Mex

Subject: double to long int type change in Mex

From: Feixiong

Date: 19 Jan, 2010 13:44:04

Message: 1 of 11

Hello,
I am now using gateway functions to call c function to speedup the program and encounterimh some problem. I am a new to matlan and c. hope you can help me!

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
...
long int *arg_val;
arg_va = (long int *) mxGetPr(prhs[0]);
....
}

i am wondering after the type change why the content is set to be zero.

Thanks

Subject: double to long int type change in Mex

From: James Tursa

Date: 19 Jan, 2010 14:43:02

Message: 2 of 11

"Feixiong " <f.liao@tue.nl> wrote in message <hj4cv4$nfh$1@fred.mathworks.com>...
> Hello,
> I am now using gateway functions to call c function to speedup the program and encounterimh some problem. I am a new to matlan and c. hope you can help me!
>
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
> ...
> long int *arg_val;
> arg_va = (long int *) mxGetPr(prhs[0]);
> ....
> }
>
> i am wondering after the type change why the content is set to be zero.
>
> Thanks

mxGetPr returns a double *. Changing this to a long int * doesn't look right. What is the type of variable that you are calling the mex function with? int32? Or what? Usually one would do this instead:

 int *arg_val;
 arg_va = (int *) mxGetData(prhs[0]);

But the above works only if the int is a 32-bit integer and prhs[0] is a class int32 variable. If you are actually trying to convert a double input into an int in the C code, you would do this:

 int arg_val;
 arg_va = *mxGetPr(prhs[0]);

You need to clarify what you are actually intending to do.

James Tursa

Subject: double to long int type change in Mex

From: Feixiong

Date: 19 Jan, 2010 15:00:22

Message: 3 of 11


> mxGetPr returns a double *. Changing this to a long int * doesn't look right. What is the type of variable that you are calling the mex function with? int32? Or what? Usually one would do this instead:
>
> int *arg_val;
> arg_va = (int *) mxGetData(prhs[0]);
>
> But the above works only if the int is a 32-bit integer and prhs[0] is a class int32 variable. If you are actually trying to convert a double input into an int in the C code, you would do this:
>
> int arg_val;
> arg_va = *mxGetPr(prhs[0]);
>
> You need to clarify what you are actually intending to do.
>
> James Tursa

hi James,

what i am intending to do is:
prhs[0]) contains a n-length of 1-D array of integer numbers, which is the index of another 2-D array input prhs[1]).

But after mxGetdata or mxGetpr, prhs[0]) was changed to double, which is can't be used as index directly.

Thanks, is there any solution for this?

Subject: double to long int type change in Mex

From: Stefan

Date: 19 Jan, 2010 15:05:04

Message: 4 of 11

How about this:

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
long int *arg_val;
double *arg_ret;

// computation
...

arg_ret = mxGetPr(prhs[0]);
*arg_ret = (double) *arg_val;

}

Regards,
Stefan

Subject: double to long int type change in Mex

From: Stefan

Date: 19 Jan, 2010 15:10:21

Message: 5 of 11


Ah, right hand side ...

to use it for indexing you need to dereference somewhere. There you can cast it to integer.
Or simply make a copy with the appropriate type.

Regards,
Stefan


"Feixiong " <f.liao@tue.nl> wrote in message <hj4he6$fra$1@fred.mathworks.com>...
>
> > mxGetPr returns a double *. Changing this to a long int * doesn't look right. What is the type of variable that you are calling the mex function with? int32? Or what? Usually one would do this instead:
> >
> > int *arg_val;
> > arg_va = (int *) mxGetData(prhs[0]);
> >
> > But the above works only if the int is a 32-bit integer and prhs[0] is a class int32 variable. If you are actually trying to convert a double input into an int in the C code, you would do this:
> >
> > int arg_val;
> > arg_va = *mxGetPr(prhs[0]);
> >
> > You need to clarify what you are actually intending to do.
> >
> > James Tursa
>
> hi James,
>
> what i am intending to do is:
> prhs[0]) contains a n-length of 1-D array of integer numbers, which is the index of another 2-D array input prhs[1]).
>
> But after mxGetdata or mxGetpr, prhs[0]) was changed to double, which is can't be used as index directly.
>
> Thanks, is there any solution for this?

Subject: double to long int type change in Mex

From: Thomas Clark

Date: 19 Jan, 2010 16:01:20

Message: 6 of 11

Possibly just the syntax error!

you assign to arg_va not to arg_val



"Feixiong " <f.liao@tue.nl> wrote in message <hj4cv4$nfh$1@fred.mathworks.com>...
> Hello,
> I am now using gateway functions to call c function to speedup the program and encounterimh some problem. I am a new to matlan and c. hope you can help me!
>
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
> ...
> long int *arg_val;
> arg_va = (long int *) mxGetPr(prhs[0]);
> ....
> }
>
> i am wondering after the type change why the content is set to be zero.
>
> Thanks

Subject: double to long int type change in Mex

From: James Tursa

Date: 19 Jan, 2010 16:03:20

Message: 7 of 11

"Feixiong " <f.liao@tue.nl> wrote in message <hj4he6$fra$1@fred.mathworks.com>...
>
> what i am intending to do is:
> prhs[0]) contains a n-length of 1-D array of integer numbers, which is the index of another 2-D array input prhs[1]).
>
> But after mxGetdata or mxGetpr, prhs[0]) was changed to double, which is can't be used as index directly.
>
> Thanks, is there any solution for this?

You still haven't directly answered my question, which was "what class is your input prhs[0]?" You say it contains integer numbers, but I think you mean it is a double class variable with integral values that are used as indexes. With this assumption:

mwSize i, j, n;
double *pr;
    :
n = mxGetNumberOfElements(prhs[0]);
pr = mxGetPr(prhs[0]);
for( i=0; i<n; i++ ) {
    j = pr[i]; // convert double to mwSize
    // j is the index you are after
}

James Tursa

Subject: double to long int type change in Mex

From: Feixiong

Date: 19 Jan, 2010 20:16:05

Message: 8 of 11

> You still haven't directly answered my question, which was "what class is your input prhs[0]?" You say it contains integer numbers, but I think you mean it is a double class variable with integral values that are used as indexes. With this assumption:
>
> mwSize i, j, n;
> double *pr;
> :
> n = mxGetNumberOfElements(prhs[0]);
> pr = mxGetPr(prhs[0]);
> for( i=0; i<n; i++ ) {
> j = pr[i]; // convert double to mwSize
> // j is the index you are after
> }
>
> James Tursa

Re:
1. the input prhs[0] is a statistic of edge set.
  for example:[1 2 15 17 ... 125, 222, 22000,..];
  let pr = mxGetPr(prhs[0]);

2. then the input prhs[1] is subset of edge set,
[1 2 2.1;
 2 21 10.5;
 2 29 10.2;
 ... ]

in which pr[1]:pr[2]-1 columns edges indicate links from node 1, pr[2]:pr[3]-1 columns indicate links from node 2, and so on...

thanks

Subject: double to long int type change in Mex

From: Feixiong

Date: 20 Jan, 2010 19:33:05

Message: 9 of 11


> Re:
> 1. the input prhs[0] is a statistic of edge set.
> for example:[1 2 15 17 ... 125, 222, 22000,..];
> let pr = mxGetPr(prhs[0]);
>
> 2. then the input prhs[1] is subset of edge set,
> [1 2 2.1;
> 2 21 10.5;
> 2 29 10.2;
> ... ]
>
> in which pr[1]:pr[2]-1 columns edges indicate links from node 1, pr[2]:pr[3]-1 columns indicate links from node 2, and so on...
>
> thanks


can anyone help me?

Subject: double to long int type change in Mex

From: James Tursa

Date: 21 Jan, 2010 00:09:03

Message: 10 of 11

"Feixiong " <f.liao@tue.nl> wrote in message <hj7lph$1sv$1@fred.mathworks.com>...
>
> > Re:
> > 1. the input prhs[0] is a statistic of edge set.
> > for example:[1 2 15 17 ... 125, 222, 22000,..];
> > let pr = mxGetPr(prhs[0]);
> >
> > 2. then the input prhs[1] is subset of edge set,
> > [1 2 2.1;
> > 2 21 10.5;
> > 2 29 10.2;
> > ... ]
> >
> > in which pr[1]:pr[2]-1 columns edges indicate links from node 1, pr[2]:pr[3]-1 columns indicate links from node 2, and so on...
> >
> > thanks
>
>
> can anyone help me?

Oh ... sorry. I thought this thread was done and you had your answer. I already showed you how to get the integral values from a double into an integer type in C, which is what I thought you needed. What is the current issue?

James Tursa

Subject: double to long int type change in Mex

From: Feixiong

Date: 22 Jan, 2010 10:39:02

Message: 11 of 11


> Oh ... sorry. I thought this thread was done and you had your answer. I already showed you how to get the integral values from a double into an integer type in C, which is what I thought you needed. What is the current issue?
>
> James Tursa

Re James,

Thanks. I think you are a very active problem-solver and wonder whether you can give me suggestions. I am used to the easiness of programming in Matlab, and now I have to deal with large scale route finding algorithm which is very slow in matlab. So I turn to use the mex technique for speedup. However, I am not good at c language. It takes me a lot time to program.

Do you know the shortest path algorithm and MatlabBGL. I am now using it, which is quite fast. But It can be faster by introducing other technique. I am wondering whether I can change it to bidirectional search or landmark ect..

Best,
Feixiong

Tags for this Thread

No tags are associated with 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