User`s guide

Overview of the C MEX S-Function Routines
3-25
Note You cannot access the w ork, state, input, output, and other vectors in
this routine. Use this rou tine only to validate the parameters. Addition al
processing of the parameters should be done in mdlProcessParameters.
Example: mdlCheckParameters. This example checks the first S-function
parameter to verify that it is a real nonnegative scalar:
#define PARAM1(S) ssGetSFcnParam(S,0)
#define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
static void mdlCheckParameters(SimStruct *S)
{
if (mxGetNumberOfElements(PARAM1(S)) != 1) {
ssSetErrorStatus(S,”Parameter to S-function must be a scalar”);
return;
} else if (mxGetPr(PARAM1(S))[0] < 0) {
ssSetErrorStatus(S, “Parameter to S-function must be non-negative”);
return;
}
}
#endif /* MDL_CHECK_PARAMETERS */
In addition to the above routine, you must add a call to this routine from
mdlInitializSizes to check parameters during initialization since
mdlCheckParameters is only called while the simul ation is running. To do this,
in
mdlInitializeSizes, after setting the number of parameters you expect in
your S- function by using
ssSetNumSFcnParams,usethiscode:
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
#if defined(MATLAB_MEX_FILE)
if(ssGetNumSfcnParams(s) == ssGetSFcnParamsCount(s) {
mdlCheckParameters(S);
if(ssGetErrorStates(S) != NULL) return;
} else {
return; /* Simulink will report a mismatch error. */
}
#endif
...
}
See matlabroot/simulink/src/sfun_errhdl.c for an example.