MATLAB: Is there a way to get the legacy code tool to generate code which passes the Simstruct object to a user’s C/C++ code in Simulink 7.2 (R2008b)

errorlegacy_codesimulink

I have been using legacy code tool to create interface templates between Simulink and custom C/C++ algorithm code. I would like to be able to report errors from my C/C++ code back to Simulink and in the process terminate a simulation run. The function to do this seems to be ssSetErrorStatus().
Calling ssSetErrorStatus requires a SimStruct structure as an argument. Unfortunately, the legacy code tool does not seem to provide a way to generate interface code which passes a SimStruct structure to the C code. This means that one cannot write C code which reports errors without modifying the code which legacy code tool creates.

Best Answer

The ability to get the legacy code tool to generate code which passes the SimStruct object to the user's C/C++ code is currently not available in Simulink 7.2 (R2008b).
The only workaround for this issue is to make some minor changes to your code, as illustrated in the attached ZIP file.
In the 'pythagoras_lct.c' and 'pythagoras_lct.h' files, please observe the #ifdef statements which introduce a symbol 'STANDALONE' to specify whether or not you would like access to the SimStruct structure. Please perform the following steps:
1. Execute the 'pythagoras_lct.m' file. This will generate a C-MEX S-function file using legacy code tool.
2. Open the generated 'pythagoras_lct_sfcn.c' file.
3. In the mdlOutputs function, change the legacy code function call from: "*y1 = pythagoras_lct(u1, u2);" to:
*y1 = pythagoras_lct(S, u1, u2);
including the SimStruct structure as the first argument to the function.
4. Compile the C-MEX S-function by executing the following command:
mex -DSTANDALONE pythagoras_lct_sfcn.c pythagoras_lct.c
The "-D" option defines a symbol name to the C preprocessor.
5. Generate the masked S-function block by executing:
legacy_code('slblock_generate', def);