Thread Subject: Float data to a MEX function

Subject: Float data to a MEX function

From: Cesare

Date: 21 Jul, 2009 07:22:01

Message: 1 of 8

Hi!
I was aondering: is there a way to pass float data to a MEX function? does mxGetPr only work with doubles?
Many thanks in advance,
Cesare

Subject: Float data to a MEX function

From: Bruno Luong

Date: 21 Jul, 2009 07:45:02

Message: 2 of 8

"Cesare " <cmfornewsgroup@gmail.com> wrote in message <h43qap$9ot$1@fred.mathworks.com>...
> Hi!
> I was aondering: is there a way to pass float data to a MEX function? does mxGetPr only work with doubles?

Simply cast mxGetPr to float*, or cleaner way use mxGetData (you might need to cast still).

Bruno

Subject: Float data to a MEX function

From: Cesare

Date: 21 Jul, 2009 09:23:03

Message: 3 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h43rlu$6oc$1@fred.mathworks.com>...
> "Cesare " <cmfornewsgroup@gmail.com> wrote in message <h43qap$9ot$1@fred.mathworks.com>...
> > Hi!
> > I was aondering: is there a way to pass float data to a MEX function? does mxGetPr only work with doubles?
>
> Simply cast mxGetPr to float*, or cleaner way use mxGetData (you might need to cast still).
>
> Bruno

Hi!
thanks very much. So there is no way to do it without a casting I guess. Is that very time consuming if done inside a C application?
Best,
Cesare

Subject: Float data to a MEX function

From: Bruno Luong

Date: 21 Jul, 2009 09:34:02

Message: 4 of 8

"Cesare " <cmfornewsgroup@gmail.com> wrote in message <h441dn$ffg$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h43rlu$6oc$1@fred.mathworks.com>...
> > "Cesare " <cmfornewsgroup@gmail.com> wrote in message <h43qap$9ot$1@fred.mathworks.com>...
> > > Hi!
> > > I was aondering: is there a way to pass float data to a MEX function? does mxGetPr only work with doubles?
> >
> > Simply cast mxGetPr to float*, or cleaner way use mxGetData (you might need to cast still).
> >
> > Bruno
>
> Hi!
> thanks very much. So there is no way to do it without a casting I guess. Is that very time consuming if done inside a C application?

Cast a pointer costs 0 in CPU runtime, it was done during compilation.

Bruno

Subject: Float data to a MEX function

From: Cesare

Date: 21 Jul, 2009 09:55:04

Message: 5 of 8

But what if I have the opposite situation: my MEX-written function is suppose to accept a double and the user passes a long (this is actually causing my MEX function to crash). Is there a workaround for that?
Thanks again,
Cesare


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h4422a$qg6$1@fred.mathworks.com>...
> "Cesare " <cmfornewsgroup@gmail.com> wrote in message <h441dn$ffg$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h43rlu$6oc$1@fred.mathworks.com>...
> > > "Cesare " <cmfornewsgroup@gmail.com> wrote in message <h43qap$9ot$1@fred.mathworks.com>...
> > > > Hi!
> > > > I was aondering: is there a way to pass float data to a MEX function? does mxGetPr only work with doubles?
> > >
> > > Simply cast mxGetPr to float*, or cleaner way use mxGetData (you might need to cast still).
> > >
> > > Bruno
> >
> > Hi!
> > thanks very much. So there is no way to do it without a casting I guess. Is that very time consuming if done inside a C application?
>
> Cast a pointer costs 0 in CPU runtime, it was done during compilation.
>
> Bruno

Subject: Float data to a MEX function

From: Bruno Luong

Date: 21 Jul, 2009 10:09:01

Message: 6 of 8

"Cesare " <cmfornewsgroup@gmail.com> wrote in message <h4439o$ffu$1@fred.mathworks.com>...
> But what if I have the opposite situation: my MEX-written function is suppose to accept a double and the user passes a long (this is actually causing my MEX function to crash). Is there a workaround for that?

If your MEX accepts double only, then you should check the input using mxGetClassID. More or less strict verification is really at the discretion of programmer/user.

Bruno

Subject: Float data to a MEX function

From: Cesare

Date: 21 Jul, 2009 12:06:02

Message: 7 of 8

Thanks again! I'm sorry, but I still got a problem. I'll try to explain it better.
In my code, the variable R is declared as a double pointer

double *R;

R is supposed to point to the numerical content of prhs[0]

R = mxGetPr(prhs[0]);

The problem comes when the user passes a matlab single matrix as first input, instead of a double one. The crash does not happen at this point but later in the code probably cause, since R was declared as a double pointer, I end up accessing values in the first input which do not exist. Is there a fast easy way to solve it?
Thanks a lot again,
Cesare

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h4443t$al7$1@fred.mathworks.com>...
> "Cesare " <cmfornewsgroup@gmail.com> wrote in message <h4439o$ffu$1@fred.mathworks.com>...
> > But what if I have the opposite situation: my MEX-written function is suppose to accept a double and the user passes a long (this is actually causing my MEX function to crash). Is there a workaround for that?
>
> If your MEX accepts double only, then you should check the input using mxGetClassID. More or less strict verification is really at the discretion of programmer/user.
>
> Bruno

Subject: Float data to a MEX function

From: Bruno Luong

Date: 21 Jul, 2009 12:37:02

Message: 8 of 8

"Cesare " <cmfornewsgroup@gmail.com> wrote in message <h44ava$lia$1@fred.mathworks.com>...
> Thanks again! I'm sorry, but I still got a problem. I'll try to explain it better.
> In my code, the variable R is declared as a double pointer
>
> double *R;
>
> R is supposed to point to the numerical content of prhs[0]
>
> R = mxGetPr(prhs[0]);
>
> The problem comes when the user passes a matlab single matrix as first input, instead of a double one. The crash does not happen at this point but later in the code probably cause, since R was declared as a double pointer, I end up accessing values in the first input which do not exist. Is there a fast easy way to solve it?
> Thanks a lot again,
> Cesare

Why a bunch of back and forth question. Your Mex work with double, then you need to cast the input in double, either preprocessing from Matlab, either on your Mex.

Otherwise you need to build a Mex file that can accepts single and double, period.

Cast in Matlab is very easy to perform:

function output = dosomething(input)

if strcmp(class(input),'single')
  input=double(input);
end
output = mexsomething(input);
end

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
float Cesare 21 Jul, 2009 03:24:10
single Cesare 21 Jul, 2009 03:24:10
double Cesare 21 Jul, 2009 03:24:09
mex Cesare 21 Jul, 2009 03:24:09
rssFeed for this Thread

Contact us at files@mathworks.com