MATLAB Answers

How can I include Simulink.Parameter objects as #define in generated code even if they are not being used in the model in Real-Time Workshop 5.2 (R2008b)?

Asked by MathWorks Support Team on 30 Jul 2012

I would like to define a set of parameters in the generated C code, even if those parameters aren't used in the model. How can I make sure that even those are declared as #define?



No tags are associated with this question.


1 Answer

Answer by MathWorks Support Team on 30 Jul 2012
 Accepted answer

One easy way to do this is by creating a simple S-function which does nothing, but define its parameters as the unused Simulink.Parameters from your list. This will make sure that those get declared, without generating any extra code.

As an example, attached is a model file 'TSModel.mdl' where the Gain block uses 'param1' from the attached MAT-file 'TSData.mat'. The other two variables in the MAT file are the ones that we want to declare in the C-file along with param1, but do not use in the model. Thus, we create an S-function called 'TSfcn' that takes param2 and param3 as parameters, but is essentially blank. If you generate code for this model, you will observe that param1, param2 and param3 are all declared as #define.

The steps we followed to create such model are as follows:

1. Add the constant, gain and output port blocks, set the gain value as 'param1'.

2. Add an S-function Builder block.

3. Double-click on the S-function Builder block. Give a name for the S-function.

4. Under the 'Data Properties' tab, make the input and output ports as empty (delete the ones present there)

5. Under the 'Parameters' sub-tab in 'Data Properties', specify two parameters, namely 'Parameter1' and 'Parameter2' with Value as 'param2' and 'param3' respectively.

6. In 'Libraries' tab, blank out all fields.

7. Go to the 'Build Info' tab and make sure that the 'Generate wrapper TLC' option is checked.

8. Click on 'Build'. This will generate the C-MEX S-Function and its support files in the current directory.

9. Open the file with the .tlc extension. There, comment or remove everything except one line: %implements TSfcn "C"

10. Save the TLC file and generate code for the model.

Step 9 is needed because we do not want any code generated for the S-function, and because the S-function would expect a pointer to the parameter which will not be possible since it is a #define.

Following this workflow, you can generate #define for your parameters as well. You can add as many parameters as you want to the S-function. Please make sure to remove everything except the one line stated in Step 9 before you generate code for your model.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!