Main Content

Simulink.CodeImporter Class

Namespace: Simulink

Import custom C/C++ code into Simulink

Since R2021a

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.

example

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.

obj = Simulink.CodeImporter;

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.

s = parse(obj);
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.

obj.Options
ans = 

  Options with properties:

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

  Options with properties:

    PassByPointerDefaultSize: "-1"
           CreateTestHarness: 0
          LibraryBrowserName: "Controller Library"
   SimulateInSeparateProcess: 0
   UndefinedFunctionHandling: "FilterOut"

Methods

expand all

Examples

collapse all

Specify location and options for custom code.

Create a code importer object.

obj = Simulink.CodeImporter('PumpController');

Specify the custom code to import.

obj.CustomCode.InterfaceHeaders = ["pumpController.h"];
obj.CustomCode.IncludePaths = ["./include"];
obj.CustomCode.SourceFiles = ["src/pumpController.c" "src/utils.c"];

Specify the name to use in the Library Browser.

obj.Options.LibraryBrowserName = "Controller Library";

Parse the custom code.

s = parse(obj);
obj.ParseInfo
ans = 

  ParseInfo with properties:

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

Import the parsed code.

s = import(obj);

Version History

Introduced in R2021a