Main Content

import

Class: Simulink.CodeImporter
Namespace: Simulink

Import custom code, functions, and types into Simulink

Since R2021a

Syntax

success = import(obj)
success = import(obj, Name1,Value1,...,NameN,ValueN)

Description

success = import(obj), 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.

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

Input Arguments

expand all

Simulink Code Importer object, specified as a handle to a Simulink.CodeImporter object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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

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

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

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

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

Output Arguments

expand all

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

Examples

expand 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
parse(obj);
fcn = getFunctions(obj.ParseInfo, "Controller");
fcn.PortSpecification.ReturnArgument.Label = "control_out";

%% Import function "Controller"
import(obj, 'Functions', ["Controller"]);

Version History

Introduced in R2021a