|
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
|