Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mxArray use in C++ S-Functions

96 views
Skip to first unread message

Matthew

unread,
Aug 20, 2009, 2:23:01 PM8/20/09
to
I have a question regarding the use of mxArrays. If I have a legacy C++ S-Function that uses standard arrays i.e. float TFKin[4][4] and float TFKinTranspose[4][4] and I need to do matrix math on them (Say for example a simple Matrix Transpose), from what I have read, Simulink does all operations on mxArrays and I need to first put my data into mxArrays? Is this what has to be done or is there a much simpler way. Also I have been trying to inderstand but are mxArrays always 1 Dimensional (or called single subscript multidimensional or something like that?)

mxArray *mxRotationT, *mxRotation;

double *Rotation;

mxRotation = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS, mxREAL);

mxRotationT = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS,mxREAL);

Rotation = mxGetPr(mxRotation);

(Rotation appears to be a 16x1 at this point right?)

//Load the MxArray

Rotation[0] = TFKin[0][0];

Rotation[1] = TFKin[0][1];

Rotation[2] = TFKin[0][2];

.... Rest of the elements, could be done in for loop.

mexCallMATLAB(1, &mxRotationT, 1, &mxRotation, "transpose" );

RotationT = mxGetPr(mxRotationT);

//Use loop to Rebuild new 4,4 Matrix from RotationT (16x1?)

Lastly, what is the processing penalty to doing the above with mexCallMATLAB, is it a fairly big hit processing wise to do MATLAB calls? This example could be done hard coded but your answers would also pertain to larger operations that could not be

Thanks,
Matt

James Tursa

unread,
Aug 20, 2009, 4:33:20 PM8/20/09
to
"Matthew " <mjre...@gmail.com> wrote in message <h6k4a5$pu9$1...@fred.mathworks.com>...

> I have a question regarding the use of mxArrays. If I have a legacy C++ S-Function that uses standard arrays i.e. float TFKin[4][4] and float TFKinTranspose[4][4] and I need to do matrix math on them (Say for example a simple Matrix Transpose), from what I have read, Simulink does all operations on mxArrays and I need to first put my data into mxArrays? Is this what has to be done or is there a much simpler way.

The inputs & outputs need to be mxArrays.

> Also I have been trying to inderstand but are mxArrays always 1 Dimensional (or called single subscript multidimensional or something like that?)

mxArrays can be multidimensional. For inputs, use mxGetNumberOfDimensions and mxGetDimensions instead of mxGetM and mxGetN. For outputs, use mxCreateNumericArray, for example, instead of mxCreateNumericMatrix.

> mxArray *mxRotationT, *mxRotation;
> double *Rotation;
> mxRotation = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS, mxREAL);
> mxRotationT = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS,mxREAL);
> Rotation = mxGetPr(mxRotation);
> (Rotation appears to be a 16x1 at this point right?)
> //Load the MxArray
> Rotation[0] = TFKin[0][0];
> Rotation[1] = TFKin[0][1];
> Rotation[2] = TFKin[0][2];
> .... Rest of the elements, could be done in for loop.
> mexCallMATLAB(1, &mxRotationT, 1, &mxRotation, "transpose" );

You just created a memory leak here. mxRotationT already points to an mxArray, namely the one you created above. The mexCallMATLAB call will simply replace the pointer in mxRotationT with a new pointer to a newly created transpose result, without first destroying the current existing mxArray that it points to. Thus the memory leak. You were probably thinking that you need to somehow preallocate the result prior to the mexCallMATLAB call. You don't. Just let MATLAB create the result mxArrays for you.

> RotationT = mxGetPr(mxRotationT);
> //Use loop to Rebuild new 4,4 Matrix from RotationT (16x1?)
>
> Lastly, what is the processing penalty to doing the above with mexCallMATLAB, is it a fairly big hit processing wise to do MATLAB calls? This example could be done hard coded but your answers would also pertain to larger operations that could not be

Processing penalty for mexCallMATLAB itself is probably not very much, but for large matrices there would potentially be quite a bit of data movement involved that could slow things down quite a bit. For your small 4x4 problem it will definitely be faster to hand code the transpose yourself rather than building mxArrays and calling the transpose function through mexCallMATLAB. If you had a very large matrix to deal with, the MATLAB transpose function is quite fast and will very likely beat any hand coding you would do. For a general problem, there would have to be a trade between the faster MATLAB transpose function and the extra data movement. I don't know at what size the balance tips in favor of hand coding vs mexCallMATLAB.

James Tursa

0 new messages