Simulink.importExternalCTypes
Generate Simulink representations of custom data types defined by C or C++ code
Syntax
Description
parses the C or C++ header files (importInfo = Simulink.importExternalCTypes(headerFiles).h or .hpp)
identified by headerFiles for typedef,
struct, and enum type definitions, and
generates Simulink® representations of the types. The output,
importInfo, identifies the successfully and unsuccessfully
imported types.
You can use the Simulink representations to:
Reuse your existing algorithmic C code and, through simulation, test its interaction with your Simulink control algorithm. For an example that shows how to use the Legacy Code Tool, see Integrate C Function Whose Arguments Are Pointers to Structures.
Generate code (Simulink Coder™) that reuses the types and data that your existing code defines. You can then integrate and compile the generated and existing code into a single application. For an example, see Exchange Structured and Enumerated Data Between Generated and External Code (Embedded Coder).
Create and organize data (signals, parameters, and states) in a model by using standard data types that your organization defines in C code.
To create structures of signals in Simulink, use nonvirtual buses. See Group Signals or Messages into Virtual Buses.
To create structures of parameters, use MATLAB® structures and
Simulink.Parameterobjects. See Organize Related Block Parameter Definitions in Structures.To create enumerated data, see Use Enumerated Data in Simulink Models.
To match a primitive
typedefstatement, use aSimulink.AliasTypeobject to set parameter and signal data types in a model.
By default, the function:
Imports an enumerated type by generating a script file that derives an enumeration class from
Simulink.IntEnumType, as described in Define Simulink Enumerations. If necessary, you can then edit the class definition to customize it (for example, by implementing theaddClassNameToEnumNamesmethod).Imports a structure type by generating a
Simulink.Busobject in the base workspace.Imports a primitive
typedefstatement by generating aSimulink.AliasTypeobject in the base workspace.Interprets generic C data types, such as
intorshort, according to the word lengths of your host computer. For example, for most modern machines,inthas a 32-bit word length, so the function represents anintstructure field as a bus element that uses the Simulink data typeint32.To override this default behavior, identify your target hardware board by using the
HardwareImplementationpair argument.
For additional information about default behavior, see Tips.
generates Simulink representations of custom C data types by analyzing a model that you
identify with importInfo = Simulink.importExternalCTypes(modelName)modelName. When you use the Simulation
Target configuration parameters in a model to identify header files
for inclusion during simulation, use this syntax to import types for the purpose of
simulating the model on your host computer. The function interprets generic C data
types according to the word lengths of your host computer.
When you use this syntax, do not use pair arguments, such as
HardwareImplementation, that can conflict with the
configuration parameters of the target model. When you use such pair arguments with
this syntax, the function generates a warning.
specifies additional options using one or more name-value pair arguments. You can
use this syntax to:importInfo = Simulink.importExternalCTypes(___,Name,Value)
Specify the names of types to import by using the
Namespair argument.Control the way that Simulink stores the imported types, for example, by generating the types in a Simulink data dictionary. Use the
MATFileandDataDictionarypair arguments.Control the way that the function interprets generic C data types. Use the
HardwareImplementationpair argument.Maintain synchrony between the C-code definitions and the Simulink representations by attempting to import the updated C-code definitions again. You can choose whether to overwrite the existing Simulink representations. Use the
OverwriteandVerbosepair arguments.
Examples
Input Arguments
Name-Value Arguments
Output Arguments
Limitations
The function does not support:
C data types that do not correspond to a type that Simulink supports. For example, Simulink does not recognize an equivalent for
long double. For information about data types that Simulink supports, see Data Types Supported by Simulink.Pointer types, such as a structure that defines a field whose value is a pointer or a
typedefstatement whose base type is a pointer type.Unions.
If a structure field represents fixed-point data, or if a
typedefstatement maps to a fixed-point base type, the function sets the data type of the corresponding bus element orSimulink.AliasTypeobject to the relevant Simulink integer type (such asint16). The importer cannot determine the fixed-point scaling by parsing the C code. After using the function, you must manually specify the data type of the bus element or the base type of theSimulink.AliasTypeobject by using thefixdtfunction.
Tips
You must configure the Simulation Target configuration parameters for your model to include (
#include) the header file which defines the imported enumeration or structure type in these cases:You use the type in a MATLAB Function block. See Control Imported Bus and Enumeration Type Definitions.
You use the type in a Stateflow® chart. See Access Custom Code Variables and Functions in Stateflow Charts (Stateflow) and Integrate Custom Structures in Stateflow Charts (Stateflow).
You use the type in a C Function or C Caller block.
The type is not declared with a
typedefstatement.
Note
To import types that are not named in a
typedefstatement, ensure the Simulation Target configuration parameter Import custom code is selected. This parameter is selected by default.By default:
For an imported enumeration, because the Simulink enumeration class derives from
Simulink.IntEnumType, when you simulate or generate code from a model, the enumeration uses the integer size that is native to your target hardware. You specify the characteristics of your target hardware by using model configuration parameters such as Production device vendor and type and Native word size in production hardware.For an imported structure type:
The function imports a structure field as numerically complex only if the field uses one of the corresponding Simulink Coder structure types as the data type. For example, if a structure field in your external code uses the data type
cint8_T, the function imports the field as a bus element (Simulink.BusElementobject) whose data type isint8and whoseComplexityproperty is set to'complex'.For nested structures, the function generates a bus object for each unique structure type.
For an imported structure or enumeration type, if your external code uses a
typedefstatement to name the type, the name of the generated bus object or Simulink enumeration class matches thetypedefname. If your code does not use atypedefstatement to name the type, the name of the object or class is the tag name of the type, unless that name conflicts with a type name that is defined in atypedefstatement elsewhere in your header files, in which case the type is not imported into Simulink. If you do not specify a tag name or apply atypedefname, Simulink generates an arbitrary name for the object or class.The function configures the generated Simulink representations as imported for the purposes of simulation and code generation. For example, for bus objects, the function sets the
DataScopeproperty to'Imported'and theHeaderFileproperty to the name of your external header file. To simulate or generate code from a model that uses one of these Simulink representations, you must make your header file available to the model.
When you specify files for
Simulink.importExternalCTypesto use or generate, for example, by using theDataDictionarypair argument:If the existing files to use are in your current folder or on the MATLAB path, you do not need to specify a file path. You can specify the file name by itself.
To control the folder location of generated files, you can specify paths as well as file names. You can also use the
OutputDirpair argument.
Version History
Introduced in R2017aSee Also
Objects
Functions
Topics
- Data Types Supported by Simulink
- Specify Bus Properties with Bus Objects
- Use Enumerated Data in Simulink Models
- Use Enumerated Data in Generated Code (Simulink Coder)
- Architectural Data Editor
- Manage Replacement of Simulink Data Types in Generated Code (Embedded Coder)
- Control Data Types of Signals
- Exchange Data Between External C/C++ Code and Simulink Model or Generated Code (Simulink Coder)