How do I parameterize a lookup table so that I can select from multiple options in the generated code?

3 views (last 30 days)
I have a lot of hardware tests I want to run where I will be deploying my Simulink model, which is my control algorithm. It contains an "n-D Lookup Table" block but I do not know exactly what lookup table data I should be using, therefore I want to generate code with a few options of table data and then switch between the options to check which data I should use at startup time.
I require the selection logic to switch between the different lookup table data options to appear in the "model_initialize" section and then the "model_step" function should only have the lookup table code.
What are my options to achieve this in MATLAB R2021b (as this is the version I am using) and any other options in later releases?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 16 Jan 2024
There are a couple of ways to achieve this in MATLAB R2021b which are listed below:
"Data Store Memory" & "Initialize Function" blocks
This method utilizes the "Data Store Memory" block and the "Initialize Function" block to select and initialize the lookup table with the appropriate data. This will work in MATLAB R2021b and provides good code customization options. You can find an example model showing this in the attached ZIP file "ex3_init_function_dsm.zip" of this model from MATLAB R2023b, or you can find a MATLAB R2021b version of this in the attached model "lut_options_dsm.slx". A screenshot of this model example where the lookup table is configured to take the table data by an "Input" port is shown below:
I have linked the MATLAB R2023b documentation pages for the "Data Store Memory" and the "Initialize Function" blocks below:
  • "Data Store Memory" - https://www.mathworks.com/help/releases/R2023b/simulink/slref/datastorememory.html
  • "Initialize Function" - https://www.mathworks.com/help/releases/R2023b/simulink/slref/initializefunction.html
Startup Variants & Variant Parameters
Here we represent the variants in the data instead of in the model by creating a "Simulink.SimulinkVariable" object which will contain all variants of our table data but only allow one of the variants to be selected and included in the model itself. This also trades off model complexity for data complexity. You can find an example model and scripts showing this in the attached ZIP file "ex2_startup_variants.zip" created from MATLAB R2023b. A screenshot showing this model example where the lookup table takes the table data as a block parameter is shown below:
Please find below the MATLAB R2023b documentation page detailing variant control modes for variant parameters:
https://www.mathworks.com/help/releases/R2023b/simulink/ug/variant-control-mode-in-variant-parameters.html
However,
if you can upgrade to MATLAB R2022a or beyond
then you can use the following method:
"Parameter Writer" & "Initialize Function" blocks
From MATLAB R2022a you have been able to use the "Parameter Writer" block inside "Initialize Function" blocks, and the "Parameter Writer" block allows you to write directly to a block parameter. Therefore using these in combination you can directly set the table value for your block using the logic selection code in the "Initialize Function" block. You can find an example model displaying this implementation in the attached ZIP file "ex1_init_function_parameter_writer.zip" created in MATLAB R2023b. An image of this example model is provided below, showing that the lookup table data is taken by block parameter:
Please find below the MATLAB R2023b documentation page for the "Parameter Writer" block:
https://www.mathworks.com/help/releases/R2023b/simulink/slref/parameterwriter.html 
Note: The above options are suitable as the size of the table data does not need to change.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!