Products

1 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.