Generate Simulink representations of custom data types defined by C or C++ code
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 Signal Lines 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 a Simulink.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 the addClassNameToEnumNames
method).
Imports a structure type by generating a Simulink.Bus
object in
the base workspace.
Imports a primitive typedef
statement by generating
a Simulink.AliasType
object in the base workspace.
Interprets generic C data types, such as int
or
short
, 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 an
int
structure field as a bus element that uses
the Simulink data type int32
.
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
and
DataDictionary
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
and Verbose
pair arguments.
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.
64-bit integer types.
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.
Structures that define a field whose value has more than one dimension.
If a field value is a 1-D array, the function creates a bus element that represents a vector, not a matrix.
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 or Simulink.AliasType
object to the relevant
Simulink integer type (such as int16
). 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 the Simulink.AliasType
object by using the fixdt
function.
If a MATLAB Function block or Stateflow® chart in your model uses an imported enumeration or structure
type, configure the model configuration parameters to include
(#include
) the type definition from your external
header file. See Control Imported Bus and Enumeration Type Definitions (for a MATLAB Function
block) and Access Custom Code Variables and Functions in Stateflow Charts (Stateflow) and Integrate Custom Structures in Stateflow Charts (Stateflow) (for a chart).
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 is int8
and whose
Complexity
property is set to
'complex'
.
For nested structures, the function generates a bus object for each unique structure type.
For an imported structure type or enumeration, if your
external code uses a typedef
statement to
name the type, the name of the generated bus object or
Simulink enumeration class matches the
typedef
name. If your code does not use a
typedef
statement, the name of the object
or class is
struct_
or
type
enum_
where type
type
is the tag name of the
type. If you do not specify a tag name or apply a
typedef
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 the
HeaderFile
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 the DataDictionary
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.