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.Parameter
objects. See Organize Related Block Parameter Definitions in Structures.To create enumerated data, see Use Enumerated Data in Simulink Models.
To match a primitive
typedef
statement, use aSimulink.AliasType
object 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 theaddClassNameToEnumNames
method).Imports a structure type by generating a
Simulink.Bus
object in the base workspace.Imports a primitive
typedef
statement by generating aSimulink.AliasType
object in the base workspace.Interprets generic C data types, such as
int
orshort
, according to the word lengths of your host computer. For example, for most modern machines,int
has a 32-bit word length, so the function represents anint
structure field as a bus element that uses the Simulink data typeint32
.To override this default behavior, identify your target hardware board by using the
HardwareImplementation
pair 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
Names
pair argument.Control the way that Simulink stores the imported types, for example, by generating the types in a Simulink data dictionary. Use the
MATFile
andDataDictionary
pair arguments.Control the way that the function interprets generic C data types. Use the
HardwareImplementation
pair 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
Overwrite
andVerbose
pair arguments.
Examples
Input 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
typedef
statement whose base type is a pointer type.Unions.
If a structure field represents fixed-point data, or if a
typedef
statement maps to a fixed-point base type, the function sets the data type of the corresponding bus element orSimulink.AliasType
object 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.AliasType
object by using thefixdt
function.
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
typedef
statement.
Note
To import types that are not named in a
typedef
statement, 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.BusElement
object) whose data type isint8
and whoseComplexity
property 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
typedef
statement to name the type, the name of the generated bus object or Simulink enumeration class matches thetypedef
name. If your code does not use atypedef
statement 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 atypedef
statement 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 atypedef
name, 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
DataScope
property to'Imported'
and theHeaderFile
property 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.importExternalCTypes
to use or generate, for example, by using theDataDictionary
pair 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
OutputDir
pair argument.
Version History
Introduced in R2017a
See Also
Objects
Functions
Topics
- Data Types Supported by Simulink
- Specify Bus Properties with Bus Objects
- Use Enumerated Data in Simulink Models
- 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)