MATLAB: How to force Real-Time Workshop to define constants defined in a MATLAB file in the generated header file in Real-Time Workshop 7.1 (R2008a)

simulink coder

I want to force Real-Time Workshop to export constants that are defined in a MATLAB file associated with a top-level model. I want a MATLAB file to define the variables as in the example below:
K_TRUE = boolean(1);
K_FALSE = boolean(0);
I also want the variables to appear in the generated header file as follows:
extern const boolean_T K_FALSE; /* Variable: K_FALSE
* '<S2>/K_FALSE'
*/
extern const boolean_T K_TRUE; /* Variable: K_TRUE
* '<S2>/K_TRUE'
*/
I need the variables to appear in the header file even if they are not used in the Simulink model, but Real-Time Workshop excludes them.

Best Answer

The ability to import variables from MATLAB files into the generated code is not available in Real-Time Workshop 7.1 (R2008a).
To work around this issue, in the Configuration Parameters dialog, specify the code that you want to include in the header or source file, using the Real-Time Workshop > Custom Code parameter.
To set the parameter programmatically, execute the following MATLAB code, where “untitled” is the name of the model to be compiled:
CustomHeaderString=sprintf(['extern const boolean_T K_FALSE; /* Variable: K_FALSE ''<S2>/K_FALSE'' */\n'...
'extern const boolean_T K_TRUE; /* Variable: K_TRUE ''<S2>/K_TRUE'' */ \n']);
set_param('untitled','CustomHeaderCode',CustomHeaderString)
The generated header file will include the specified string.