Main Content

Reuse Parameter Data from External Code in the Generated Code

This example shows how to generate code that imports a parameter value from your external code.

Create External Code Files

Suppose your external code defines a vector parametermyGainswith three elements. Save the definition in your current folder in a file calledex_vector_import_src.c.

#include "ex_vector_import_decs.h" my_int8 myGains[3] = { 2, 4, 6 };

Save the declaration in your current folder in a file calledex_vector_import_decs.h.

#include "ex_vector_import_cust_types.h" extern my_int8 myGains[3];

Save the data type definitionmy_int8in your current folder in a file calledex_vector_import_cust_types.h.

typedef signed char my_int8;

Import Parameter Value for Simulation

In your current folder, right-click the fileex_vector_import_src.cand selectImport Data.

In the Import dialog box, setOutput TypetoNumeric Matrix.

Set the name of the generated MATLAB variable totempVar.

Select only the parameter values (2,4, and6) to import.

Import the data by clicking the green check mark. The MATLAB variabletempVarappears in the base workspace.

Alternatively, use the command prompt to manually createtempVar.

tempVar = [2;4;6];

At the command prompt, create aSimulink.Parameterobject that representsmyGains.

myGains = Simulink.Parameter(tempVar);

Create and Configure Model

Create the modelex_vector_import.

open_system('ex_vector_import')

Open theEmbedded Coder应用程序。

On theModelingtab, clickModel Data Editor.

In the Model Data Editor, inspect theParameterstab.

Use theValuecolumn to set the value of theGainparameter in the Gain block tomyGains.

Click theShow/refresh additional informationbutton. The data table now contains a row that represents the parameter object,myGains.

Use theData Typecolumn to set the data type ofmyGainstomy_int8.

For the other row (which represents theGain参数的获得块),集Data TypetoInherit: Inherit from 'Gain'. With this setting, theGainparameter inherits the data typemy_int8frommyGains.

Set theChange viewdrop-down list toCode.

Set these properties formyGains:

  • Storage ClasstoImportFromFile

  • Header Filetoex_vector_import_decs.h

Alternatively, use these commands at the command prompt to configure the object and the block:

set_param('ex_vector_import/Gain',“获得”,'myGains',...'ParamDataTypeStr','Inherit: Inherit from ''Gain''') myGains.DataType ='my_int8'; myGains.CoderInfo.StorageClass ='Custom'; myGains.CoderInfo.CustomStorageClass ='ImportFromFile'; myGains.CoderInfo.CustomAttributes.HeaderFile ='ex_vector_import_decs.h';

At the command prompt, create aSimulink.AliasTypeobject to represent your custom data typemy_int8. Set theDataScopeandHeaderFileproperties to import the type definition from your external code.

my_int8 = Simulink.AliasType('int8'); my_int8.DataScope ='Imported'; my_int8.HeaderFile ='ex_vector_import_cust_types.h';

Set model configuration parameterSource filestoex_vector_import_src.c.

set_param('ex_vector_import','CustomSource','ex_vector_import_src.c')

Generate and Inspect Code

Generate code.

rtwbuild('ex_vector_import')
### Starting build procedure for: ex_vector_import ### Successful completion of build procedure for: ex_vector_import Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================================= ex_vector_import Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 15.927s

The generated fileex_vector_import.hincludes the external header filesex_vector_import_decs.handex_vector_import_cust_types.h, which contain the parameter variable declaration (myGains) and custom type definition (my_int8).

file = fullfile('ex_vector_import_ert_rtw','ex_vector_import.h'); rtwdemodbtype(file,'#include "ex_vector_import_cust_types.h"',...'#include "ex_vector_import_cust_types.h"',1,1) rtwdemodbtype(file,'/* Includes for objects with custom storage classes. */',...'#include "ex_vector_import_decs.h"',1,1)
#include "ex_vector_import_cust_types.h" /* Includes for objects with custom storage classes. */ #include "ex_vector_import_decs.h"

The generated code algorithm in the modelstepfunction in the generated fileex_vector_import.cusesmyGainsfor calculations.

file = fullfile('ex_vector_import_ert_rtw','ex_vector_import.c'); rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)
/* Model step function */ void ex_vector_import_step(void) { /* Outport: '/Out1' incorporates: * Gain: '/Gain' * Inport: '/In1' */ rtY.Out1[0] = (real_T)myGains[0] * rtU.In1; rtY.Out1[1] = (real_T)myGains[1] * rtU.In1; rtY.Out1[2] = (real_T)myGains[2] * rtU.In1; }

The generated code does not define (allocate memory for) or initialize the global variablemyGainsbecause the data scope of the corresponding parameter object is imported.

When you simulate the model in Simulink, the model uses the value stored in theValueproperty of the parameter object. However, if you use external mode simulation, the external executable begins the simulation by using the value from your code. SeeConsiderations for Other Modeling Goals.

Related Topics