How can I pass a matrix as a parameter into a C-MEX S-function?

3 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Feb 2013
In order to pass a matrix as a parameter into a C-MEX S-function, you want to use the ssGetSFcnParam macro to get the parameter as follows:
#define PARAM_ARG ssGetSFcnParam(S, 0) %zero will be the parameter index
Then, in mdlOutputs, you can do the following to access the parameter:
real_T *p = mxGetPr(PARAM_ARG);
You would then index into each element of "p" to get each element of the matrix.
NOTE: Matrices are stored in column major order in S-functions. So you will have to access each matrix element by one index not two indices.For example, if the parameter is a [2x2] matrix
- -
| p[0] p[2] |
| p[1] p[3] |
- -
Output elements are stored as follows:
p[0] --> row = 0, col = 0
p[1] --> row = 1, col = 0
p[2] --> row = 0, col = 1
p[3] --> row = 1, col = 1
For a more detailed decription of how S-functions handle matrices, see the sfun_matadd.c example that is available in the $MATLAB/simulink/src directory (till release MATLAB R2009a).
In the MATLAB R2012b release, can refer to the following SFunction Demo :
>> sfcndemo_stvmgmain
which, implements a time-varying matrix gain

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!