Main Content

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.

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 file 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 ConfigurationInterface.

open_system('ConfigurationInterface')
set_param('ConfigurationInterface/Table1','DataSpecification','Table and breakpoints');

Open the Table1 block dialog box. The block refers to variables, T1Data and T1Break, in the model 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.

mws = get_param('ConfigurationInterface','modelworkspace');
T1Data = mws.getVariable('T1Data');
T1Break = mws.getVariable('T1Break');
T1Data = Simulink.Parameter(T1Data);
T1Break = Simulink.Parameter(T1Break);

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('ConfigurationInterface','AllowSymbolicDim','on');

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

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

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

mws.assignin('T1Data',T1Data);
mws.assignin('T1Break',T1Break);
cm = coder.mapping.utils.create('ConfigurationInterface');
setDataDefault(cm,'ModelParameters','StorageClass','Default');
setModelParameter(cm,'Table1','StorageClass','Model default');
setModelParameter(cm,'Table2','StorageClass','Model default');
setModelParameter(cm,'T1Data','StorageClass','ImportFromFile','HeaderFile','ex_vec_symdim_decs.h');
setModelParameter(cm,'T1Break','StorageClass','ImportFromFile','HeaderFile','ex_vec_symdim_decs.h');

Generate and Inspect Code

Generate code from the model.

The generated code algorithm is in the model step function in the generated file ConfigurationInterface.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.

For more information about symbolic dimensions, see Implement Symbolic Dimensions for Array Sizes in Generated Code.

Related Topics