Main Content

Integrate Artifact with Target Language Application

MATLAB® Compiler SDK™ allows you to package MATLAB functions for use in applications written in other programming languages. For an overview on packaging and deploying MATLAB code, see Steps for Deployment with MATLAB Compiler.

Several deployment targets are available with MATLAB Compiler SDK.

  • .NET

  • C/C++

  • Python®

  • Java®

  • COM

  • MATLAB Production Server™

  • Docker®

Meet Requirements and Prepare Development Environment

Before you begin to develop code in your target language, you must meet the requirements and set up your development environment. This preparation involves installing the necessary software tools, including a code editor or integrated development environment (IDE). For some targets, such as .NET or Java, the compiler invokes the corresponding third-party compiler to create the artifact, so you must set up your development environment to work with the target language.

For general information about MATLAB Compiler SDK requirements, see Supported Hardware and Software. For specific requirements and development guidelines, see the documentation for your target language.

Choose API for .NET or C++

For .NET and C++ applications, MATLAB Compiler SDK provides two options to handle data exchange between the application and deployed MATLAB functions. Before packaging a .NET assembly or C++ shared library, you must select the API that best matches the requirements for your project.

The .NET target has the following API options.

  • MWArray API — legacy option that uses MWArray classes for arguments and return values

  • MATLAB Data API — modern option that uses a mapping between MATLAB and target language data types

For more details, see Choose .NET Deployment Option.

The C++ target has the following API options.

  • mwArray API — legacy option that uses mxArray classes for arguments and return values

  • MATLAB Data API — modern option that uses a mapping between MATLAB and target language data types

For more details, see Choose C++ Deployment Option.

Package Code for Target

You can use the following options to package MATLAB code.

  • The compiler.build and compiler.package functions, which allow you to package MATLAB code at the command line

  • The deploytool compiler apps, which allow you to package MATLAB code using a graphical interface

  • The mcc function, which allows you to package MATLAB code at the command line and offers additional options to control the packaging process

For information on which packaging method to use, see Choose Deployment Option.

(Optional) Generate Sample Application Code

During packaging, MATLAB Compiler SDK can generate C++, .NET, Java, or Python code that demonstrates how to call your MATLAB exported function in the target language. You can use this sample code to test the compiled artifact or as a base to implement your own application. For more details, see Create Sample Code to Call Exported Function.

For an example on using application code generated by MATLAB Compiler SDK, see Generate Python Package and Build Python Application.

Write Application Code in Target Language

After packaging MATLAB code, distribute the generated files to the development environment. For information on which files to distribute, see Distribute MATLAB Compiler SDK Files to Application Developers. Then, write application code in the target language that uses the packaged MATLAB functionality.

For deployment to MATLAB Production Server or to Docker as a microservice, access the packaged MATLAB functionality using a client application that sends and receives data using HTTP requests. For details, see the documentation for your desired target.

For other target languages, application code typically uses the following structure.

  • Includes the compiled MATLAB artifact

  • Includes header files or other required libraries, such as the Python matlab package

  • Initializes MATLAB Runtime, if necessary

  • Uses a try-catch block to handle exceptions

  • Initializes an instance of the compiled artifact as an object

  • Creates data objects for each input argument as needed

  • Calls the MATLAB function using the artifact object instance

  • Prints any output

  • Terminates the object instances and MATLAB Runtime

For specific implementation details, see the documentation for your target language.

Deploy Application and MATLAB Runtime

After integrating the artifact with your target language application and testing the application code against MATLAB Runtime, distribute the application along with all required files and dependencies to the end user machine for deployment.

To generate an installer that installs your compiled artifact along with MATLAB Runtime, package your code using the Library Compiler app or use a function such as compiler.package.installer or compiler.runtime.customInstaller.

By default, installers only contain files generated by MATLAB Compiler SDK, including sample code, and any additional files added during packaging. Installers do not include any custom application code written after packaging. You can add files, such as a target language application, to an installer by adding them to the Library Compiler project before packaging or by using the AdditionalFiles option with a compiler.build or compiler.package function.

Note

Some targets, such as .NET, and Java, require you to compile application code before execution. To ensure end users can run the application, compile your code into an executable bundle and then distribute the files.

As an alternative to using an installer, you can distribute generated files manually to the end user. For details, see Distribute MATLAB Compiler SDK Files to Application Developers. If you distribute manually, you must also ensure MATLAB Runtime is accessible on the target machine. For more information, see About MATLAB Runtime.

See Also

| | | |

Related Topics