Contenu principal

Create a Generic COM Component with MATLAB Code

Supported platform: Windows®

This example shows how to create a generic COM component using a MATLAB® function and integrate it into an application. The target system does not require a licensed copy of MATLAB.

Prerequisites

  • Verify that you have the Windows 10 SDK kit installed. For details, see Windows 10 SDK.

  • Verify that you have MinGW-w64 installed. To install it from the MathWorks® File Exchange, see MATLAB Support for MinGW-w64 C/C++ Compiler.

    To ensure that MATLAB detects the Windows 10 SDK kit and MinGW-w64, use the following command:

    mbuild -setup -client mbuild_com

  • Verify that you have Microsoft® Visual Studio® installed.

  • End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want packaged. For this example, open makesquare.m located in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareComp.

function y = makesquare(x)
y = magic(x);

At the MATLAB command prompt, enter makesquare(5).

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

Create COM Component Using compiler.build.COMComponent

Package the function into a COM component using the compiler.build.comComponent function.

  1. Copy the file makesquare.m located in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareComp to your working directory. For example, if you are using Visual Studio version 15, type:

    copyfile(fullfile(matlabroot,'toolbox','dotnetbuilder','Examples', ...
        'VS15','COM','MagicSquareExample','MagicSquareComp','makesquare.m'));
    appFile = which('makesquare.m')
  2. Build the COM component using the compiler.build.comComponent function. Use name-value arguments to specify the component name and class name.

    buildResults = compiler.build.comComponent(appFile, ...
    'ComponentName','MagicSquareComp', ...
    'ClassName','Class1');

    You can specify additional options in the compiler.build command by using name-value arguments. For details, see compiler.build.comComponent.

    The compiler.build.Results object buildResults contains information on the build type, generated files, included support packages, and build options.

    The build function generates files within a folder named MagicSquareCompcomComponent in your current working directory. For information on the files generated, see Files Generated After Packaging MATLAB Functions.

    Note

    The generated component does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.installer.

Integrate into COM Application

To integrate your COM component into an application, see Creating the Microsoft Visual Basic Project.

See Also

|

Topics