Main Content

import

Import custom code, functions, and types into Simulink

    Description

    success = obj.import(), where obj is an object of class Simulink.CodeImporter, parses the specified custom code and imports it into Simulink® with default import options. If the custom code was not previously parsed or if the custom code or the settings of obj.CustomCode have changed since the custom code was parsed, then the ParseInfo property of obj is populated or repopulated with the information resulting from the parse.

    example

    success = obj.import(Name1,Value1,...,NameN,ValueN) parses the specified custom code and imports it into Simulink with options based on the specified name-value arguments.

    Examples

    collapse all

    Specify location and options for custom code, then import the code into Simulink.

    %% Create code importer object
    obj = Simulink.CodeImporter;
    
    %% Set library file name and output folder
    obj.LibraryFileName = "pumpController";
    obj.OutputFolder = ".";
    
    %% Set the custom code to import
    obj.CustomCode.InterfaceHeaders = ["pumpController.h"];
    obj.CustomCode.IncludePaths = ["./include"];
    obj.CustomCode.SourceFiles = ["src/pumpController.c" "src/utils.c"];
    
    %% Parse custom code and configure function
    obj.parse();
    fcn = obj.ParseInfo.getFunctions("Controller");
    fcn.PortSpecification.ReturnArgument.Label = "control_out";
    
    %% Import function "Controller"
    obj.import('Functions', ["Controller"]);

    Input Arguments

    collapse all

    Name-Value Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example: obj.import('Functions',["Controller"],'EntryFunctionsOnly','On');

    Option to limit import to entry-point functions, specified as 'On' or 'Off'. If specified as 'On', only entry-point functions from the custom code, that is, functions not called by other functions in the custom code, are imported into the Simulink library. Otherwise, all available custom code functions are imported.

    Data Types: char | string

    Functions to import, specified as a cell array of character vectors or a string array. Specify this argument to limit import to the named custom code functions. Otherwise, all available custom code functions or all available entry-point custom code functions are imported.

    Data Types: char | string

    Additional types to import, specified as a cell array of character vectors or a string array. All types required by the imported functions are imported. Additional types are defined in the interface headers.

    Data Types: char | string

    Option to overwrite existing Simulink library with the same name as the name specified in the LibraryFileName property, specified as 'On' or 'Off'. If set to 'Off' and there is an existing Simulink library with the same name, custom code is imported into the existing library without changing block settings applied in the library after previous imports of custom code. If set to 'On', the existing library is overwritten by the library created from the custom code import, and any prior changes to the existing library are lost. If there is no existing Simulink library with the same name, a new library is created.

    Data Types: char | string

    Option to add custom code and generated artifacts to existing MATLAB project, specified as a file path. Artifacts include a library file for imported functions and a data dictionary for imported types. If obj.Options.CreateTestHarness is set to true and Simulink Test™ is installed, artifacts also include a Simulink Test file.

    Data Types: char | string

    Output Arguments

    collapse all

    Whether import is successful, specified as a logical scalar. Simulink throws an exception if the import is unsuccessful.

    Introduced in R2021a