Main Content

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 function generates the following files within a folder named MagicSquareCompcomComponent in your current working directory:

    • magicsquare.def

    • magicsquare.rc

    • magicsquare_1_0.dll

    • readme.txt

    • requiredMCRProducts.txt

    • unresolvedSymbols.txt

    • Class1_com.cpp — C++ source code file that defines the class.

    • Class1_com.hpp — C++ header file that defines the class.

    • dlldata.c — C source code file that contains entry points and data structures required by the class factory for the DLL.

    • GettingStarted.html — HTML file that contains steps on installing COM components.

    • includedSupportPackages.txt — Text file that contains information on included support packages.

    • MagicSquareComp.def — Module definition file that defines which functions to include in the DLL export table.

    • MagicSquareComp.rc — Resource script file that describes the resources used by the component.

    • MagicSquareComp_1_0.dll — Dynamic-link library file.

    • MagicSquareComp_dll.cpp — C++ source code file that contains helper functions.

    • MagicSquareComp_idl.h — C++ header file.

    • MagicSquareComp_idl.idl — Interface definition language file.

    • MagicSquareComp_idl.tlb — Type library file that contains information about the COM object properties and methods.

    • MagicSquareComp_idl_i.c — C source code file that contains the IIDs and CLSIDs for the IDL interface.

    • MagicSquareComp_idl_p.c — C source code file that contains the proxy stub code for the IDL interface.

    • mccExcludedFiles.log — Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.

    • mwcomtypes.h — C++ header file that contains the definitions for the interfaces.

    • mwcomtypes_i.c — C source code file that contains the IIDs and CLSIDs.

    • mwcomtypes_p.c — C source code file that contains the proxy stub code.

    • readme.txt — Text file that contains deployment information.

    • requiredMCRProducts.txt — Text file that contains product IDs of products required by MATLAB Runtime to run the application.

    • unresolvedSymbols.txt — Text file that contains information on unresolved symbols.

    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