Main Content

Definition and Declaration of Signal Data

This example shows how to export the definition and declaration of a global variable that the generated code uses as a signal.

C Construct

float mySig;

extern float mySig;

Procedure

1. Open the example model ex_defn_decl.

open_system('ex_defn_decl');

2. In the Model Data Editor, open the Inports/Outports tab.

3. In the Model Data Editor, for the Inport block, set Data Type to single.

set_param('ex_defn_decl/In1','OutDataTypeStr','single');

4. Open the Embedded Coder app.

5. In the C Code tab, select Code Interface > Individual Element Code Mappings.

6. In the Code Mappings editor, on the Inports tab, set Storage class for the Inport block to ExportToFile.

7. In the Property Inspector, set Identifier to mySig, Header File to myDecls.h, and Definition File to myDefns.c.

cm = coder.mapping.api.get('ex_defn_decl');
setInport(cm,'In1','StorageClass','ExportToFile','Identifier','mySig','HeaderFile','myDecls.h','DefinitionFile','myDefns.c');

8. Generate code from the model.

currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('slbuild(''ex_defn_decl'')');

Results

The generated header file myDecls.h declares the global variable mySig by using the extern keyword.

file = fullfile('ex_defn_decl_ert_rtw','myDecls.h');
rtwdemodbtype(file,'/* Declaration for', ...
    'extern real32_T mySig',1,1);
/* Declaration for custom storage class: ExportToFile */
extern real32_T mySig;                 /* '<Root>/In1' */

The generated source file myDefns.c defines and initializes mySig.

file = fullfile('ex_defn_decl_ert_rtw','myDefns.c');
rtwdemodbtype(file,'/* Definition for', ...
    'real32_T mySig;',1,1);
/* Definition for custom storage class: ExportToFile */
real32_T mySig;                        /* '<Root>/In1' */

Related Topics