Main Content

Simulink.CodeImporter class

Package: Simulink

Import custom C/C++ code into Simulink

Description

Use the Simulink.CodeImporter class to import custom C/C++ code into Simulink® for modeling, verification, and validation. Instances of this class are used to specify custom code to import into Simulink.

The Simulink.CodeImporter class is a handle class.

Creation

Description

obj = Simulink.CodeImporter() creates an instance of the CodeImporter with the LibraryFileName property set to "untitled".

obj = Simulink.CodeImporter(LibName), where LibName is a file name chosen by the user and specified as a string or character vector, creates an instance of the CodeImporter with the LibraryFileName property set to LibName.

Properties

expand all

Name of Simulink library file and generated artifacts created for the imported code, specified as a string or a character vector. Name must be a valid MATLAB® variable name. The same file name is used with different extensions for the data dictionary and other artifacts generated when code is imported.

Example: "pumpController"

Data Types: string | character vector

Location of generated library file and generated artifacts, specified as a string or a character vector. If left blank, the current MATLAB folder is used.

File locations that are part of the CustomCode property can be specified relative to this folder.

Example: "C:\HeatPump\Controller"

Data Types: string | character vector

C or C++ code files and associated properties to import, specified as an object of class Simulink.CodeImporter.CustomCode. Use this property to specify code importing options, such as source and header files, folder paths, libraries, and compiler and linker flags. For information, see Simulink.CodeImporter.CustomCode.

%% Create code importer object
obj = Simulink.CodeImporter;

%% Set the custom code to import
obj.CustomCode.InterfaceHeaders = ["pumpController.h"];
obj.CustomCode.IncludePaths = ["./include"];
obj.CustomCode.SourceFiles = ["src/pumpController.c" "src/utils.c"];
obj.CustomCode
ans = 

  CustomCode with properties:

                SourceFiles: ["src/pumpController.c"    "src/utils.c"]
           InterfaceHeaders: "pumpController.h"
               IncludePaths: "./include"
                  Libraries: [1×0 string]
                    Defines: [1×0 string]
                   Language: "C"
              CompilerFlags: [1×0 string]
                LinkerFlags: [1×0 string]
    GlobalVariableInterface: 0
        FunctionArrayLayout: NotSpecified

Information regarding the parsed custom code, specified as an object of class Simulink.CodeImporter.ParseInfo. This property is read-only. Parsing needs to be successful to obtain information regarding custom code. For information, see Simulink.CodeImporter.ParseInfo.

Example:

s = obj.parse;
obj.ParseInfo
ans = 

  ParseInfo with properties:

               Success: 1
    AvailableFunctions: ["Controller"    "setFanTempThreshold"    "setPumpTempThreshold"]
        EntryFunctions: ["Controller"    "setFanTempThreshold"    "setPumpTempThreshold"]
        AvailableTypes: "pump_control_bus"
                Errors: []

Additional options used by Code Importer during import for library creation, specified as an object of class Simulink.CodeImporter.Options. Use this object to change the default values for the size of an argument passed by a pointer to a function, and the Simulink library browser name. For information, see Simulink.CodeImporter.Options.

Example:

>> obj.Options
ans = 

  Options with properties:

    PassByPointerDefaultSize: "-1"
           CreateTestHarness: 0
          LibraryBrowserName: ""
>> obj.Options.LibraryBrowserName = "Controller Library";
>> obj.Options
ans = 

  Options with properties:

    PassByPointerDefaultSize: "-1"
           CreateTestHarness: 0
          LibraryBrowserName: "Controller Library"

Methods

expand all

Examples

Import Custom Code

Specify location and options for custom code.

%% Create code importer object
obj = Simulink.CodeImporter('PumpController');

%% Set the custom code to import
obj.CustomCode.InterfaceHeaders = ["pumpController.h"];
obj.CustomCode.IncludePaths = ["./include"];
obj.CustomCode.SourceFiles = ["src/pumpController.c" "src/utils.c"];

%% Specify name for Library Browser
obj.Options.LibraryBrowserName = "Controller Library";

Parse custom code and examine results.

%% Parse custom code
s = obj.parse;
obj.ParseInfo
ans = 

  ParseInfo with properties:

               Success: 1
    AvailableFunctions: ["Controller"    "setFanTempThreshold"    "setPumpTempThreshold"]
        EntryFunctions: ["Controller"    "setFanTempThreshold"    "setPumpTempThreshold"]
        AvailableTypes: "pump_control_bus"
                Errors: []

Import parsed code into Simulink

%% Import code
s = obj.import;
Introduced in R2021a