MATLAB Examples

Import Parameter Data with Conditionally Compiled Dimension Length

Suppose your external code conditionally allocates memory for and initializes lookup table and breakpoint set data based on a dimension length that you specify as a #define macro. This example shows how to generate code that imports this external global data.

Contents

Create External Code Files

Save the definition of the breakpoint set data T1Break and lookup table data T1Data in your current folder in a file called ex_vec_symdim_src.c. These global variables have either 9 or 11 elements depending on the value of the macro bpLen.

#include "ex_vec_symdim_decs.h"

#if bpLen == 11
double T1Break[bpLen] = { 
    -5.0, 
    -4.0, 
    -3.0, 
    -2.0, 
    -1.0, 
    0.0, 
    1.0, 
    2.0, 
    3.0, 
    4.0, 
    5.0 
} ;
    
double T1Data[bpLen] = { 
    -1.0, 
    -0.99, 
    -0.98, 
    -0.96, 
    -0.76, 
    0.0, 
    0.76, 
    0.96, 
    0.98, 
    0.99, 
    1.0 
} ; 
#endif

#if bpLen == 9
double T1Break[bpLen] = { 
    -4.0, 
    -3.0, 
    -2.0, 
    -1.0, 
    0.0, 
    1.0, 
    2.0, 
    3.0, 
    4.0
} ;
    
double T1Data[bpLen] = { 
    -0.99, 
    -0.98, 
    -0.96, 
    -0.76, 
    0.0, 
    0.76, 
    0.96, 
    0.98, 
    0.99
} ; 
#endif

Save the declarations of the variables and the definition of the macro in your current folder in a file called ex_vec_symdim_decs.h.

#define bpLen 11

extern double T1Break[bpLen];  
extern double T1Data[bpLen];

Explore and Configure Example Model

Open the example model rtwdemo_basicsc.

open_system('rtwdemo_basicsc')

Open the Table1 block dialog box. The block refers to variables, T1Data and T1Break, in the base workspace. These variables store the lookup table and breakpoint set data with 11 elements.

At the command prompt, convert the variables to Simulink.Parameter objects.

T1Data = Simulink.Parameter(T1Data);
T1Break = Simulink.Parameter(T1Break);

Configure the objects to import the data definitions from your external code.

T1Data.CoderInfo.StorageClass = 'Custom';
T1Data.CoderInfo.CustomStorageClass = 'ImportFromFile';
T1Data.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h';

T1Break.CoderInfo.StorageClass = 'Custom';
T1Break.CoderInfo.CustomStorageClass = 'ImportFromFile';
T1Break.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h';

At the command prompt, create a Simulink.Parameter object to represent the custom macro bpLen.

bpLen = Simulink.Parameter(11);
bpLen.Min = 9;
bpLen.Max = 11;
bpLen.DataType = 'int32';
bpLen.CoderInfo.StorageClass = 'Custom';
bpLen.CoderInfo.CustomStorageClass = 'ImportedDefine';
bpLen.CoderInfo.CustomAttributes.HeaderFile = 'ex_vec_symdim_decs.h';

Use bpLen to set the dimensions of the lookup table and breakpoint set data. Configure the model to enable symbolic dimensions by selecting the configuration parameter Allow symbolic dimension specification.

T1Data.Dimensions = '[1 bpLen]';
T1Break.Dimensions = '[1 bpLen]';
set_param('rtwdemo_basicsc','AllowSymbolicDim','on')

Set Configuration Parameters > Code Generation > Custom Code > Additional build information > Source files to ex_vec_symdim_src.c.

set_param('rtwdemo_basicsc','CustomSource','ex_vec_symdim_src.c')

To generate code with custom storage classes such as ImportFromFile, you must use an ERT-based system target file. Set Configuration Parameters > Code Generation > System target file to ert.tlc.

set_param('rtwdemo_basicsc','SystemTargetFile','ert.tlc')

Generate and Inspect Code

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

The generated code algorithm is in the model step function in the generated file rtwdemo_basicsc.c. The algorithm passes T1Break, T1Data, and bpLen as argument values to the function that performs the table lookup. In this case, bpLen controls the upper bound of the binary search that the function uses.

file = fullfile('rtwdemo_basicsc_ert_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'.X = look1_iflf_binlx','bpLen - 1U',1,1)
    rtwdemo_basicsc_DW.X = look1_iflf_binlx(rtwdemo_basicsc_U.input2,
      (&(T1Break[0])), (&(T1Data[0])), bpLen - 1U) * 2.0F;

For more information about symbolic dimensions, see docid:ecoder_ug.bu3c8bh-1.