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:
Pass float data to a C MEX function

Subject: Pass float data to a C MEX function

From: Thomas Clark

Date: 16 Dec, 2009 12:12:19

Message: 1 of 5

Hi all,

I have a couple of *huge* variables in my MATLAB workspace, which I'm passing to a C mex function.

The topic of passing floats has been handled to some extent in a previous thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256595#666914

... but the conclusion was that variables should be explicitly cast to double() in MATLAB. This is likely to cause an out of memory error in my case, as the single-precision variables take up more than half my memory.

So to access double data in a c mex file:
double *ptr_to_myarray
ptr_to_myarray = mxGetPr(prhs[0])
a_useful_number = ptr_to_myarray[index]

But where the mxArray pointed to by prhs[0] has already been cast to single; and cannot be cast to double, I'm convinced that the above approach, simply redefined as a float type, won't work.

It has been hinted that mxGetData could be used to achieve this, instead of mxGetPr, but it returns a pointer of type void *.

How can I get a pointer of type float * from one of type void *? Does simply casting the pointer work?

Thanks for any help!

Tom Clark

Subject: Pass float data to a C MEX function

From: Rune Allnor

Date: 16 Dec, 2009 14:21:28

Message: 2 of 5

On 16 Des, 13:12, "Thomas Clark" <t.cl...@remove.spamcantab.net>
wrote:
> Hi all,
>
> I have a couple of *huge* variables in my MATLAB workspace, which I'm passing to a C mex function.
>
> The topic of passing floats has been handled to some extent in a previous thread:http://www.mathworks.com/matlabcentral/newsreader/view_thread/256595#...
>
> ... but the conclusion was that variables should be explicitly cast to double() in MATLAB. This is likely to cause an out of memory error in my case, as the single-precision variables take up more than half my memory.

So the situation is that you have single-precision data
in your matlab workspace and want to process them as
float type data in your C file?

> So to access double data in a c mex file:
> double *ptr_to_myarray
> ptr_to_myarray = mxGetPr(prhs[0])
> a_useful_number = ptr_to_myarray[index]
>
> But where the mxArray pointed to by prhs[0] has already been cast to single; and cannot be cast to double, I'm convinced that the above approach, simply redefined as a float type, won't work.

> It has been hinted that mxGetData could be used to achieve this, instead of mxGetPr, but it returns a pointer of type void *.
>
> How can I get a pointer of type float * from one of type void *? Does simply casting the pointer work?

Yes, it does. The reason mxGetData returns a void is that
this requires the user to typcast the pointer to something
useful. The C type 'void*' means 'pointer to data of
as of yet unspecified type'.

Mind you, it is *your* responsibility as programmer to
ensure that the data contained in the array are indeed
of the type you tell the compiler they are. If you
typecast to float and the data in reality are something
else, misery and mayhem ensues.

Rune

Subject: Pass float data to a C MEX function

From: Thomas Clark

Date: 16 Dec, 2009 14:59:07

Message: 3 of 5

Rune,

Thanks very much - that's a useful little gem of knowledge :)

I'm using the mxIsSingle function to throw an error if the mxArray is of the wrong type. Any other interested readers of this post can type mxIs* into their MATLAB help browser, to find c mx functions to perform a runtime check that the input data is of a suitable type.

Cheers!

Tom Clark

Subject: Pass float data to a C MEX function

From: Jan Simon

Date: 16 Dec, 2009 16:04:06

Message: 4 of 5

Dear Thomas!

> How can I get a pointer of type float * from one of type void *? Does simply casting the pointer work?

Yes.

Meta-reply: I'm definitely impressed your exact question! It is really seldom, that one can reply a straight "yes" or "no". Seriously.

Jan

Subject: Pass float data to a C MEX function

From: Thomas Clark

Date: 16 Dec, 2009 17:14:22

Message: 5 of 5

He he, thanks Jan - it does make a difference, doesn't it!

Confirmation: This is now working for float/single precision data. I presume that it will work for other data types too, noting that the programmer should force a check to prevent possible segmentation violations or arithmetic errors.

Thanks!

Tom

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