Create C++ MATLAB Data API Shared Library Header from Strongly Typed MATLAB Function - MATLAB & Simulink (original) (raw)

Main Content

This example shows how to create a C++ MATLABĀ® Data API shared library header from a strongly typed MATLAB function and integrate it with sample C++ application code. The target system does not require a licensed copy of MATLAB to run the application.

Prerequisites

Create Function in MATLAB

Create a MATLAB file named stronglyTypedFactorial.m with the following code:

function fact = stronglyTypedFactorial(n) arguments n (1,1) uint64 {mustBeReal, mustBeLessThan(n,22)} end fact = 1; for i = 1:n fact = fact*i; end end

Test the function at the MATLAB command prompt.

stronglyTypedFactorial(5)

Generate C++ Shared Library Header Using the mcc Command

Generate the C++ shared library header using the mcc command. At the command prompt, type:

if ~exist('output/cpp','dir') mkdir output/cpp end mcc -W 'cpplib:stronglyTypedFactorial,generic' stronglyTypedFactorial.m -d output/cpp

The following files are created in the v2 >generic_interface folder:

The stronglyTypedFactorialv2.hpp header file contains a C++ data array that accepts an argument of type uint64_t.

matlab::data::Array stronglyTypedFactorial(std::shared_ptr<MATLABControllerType> _matlabPtr, uint64_t n)

This uint64_t mapping matches the strongly typeduint64 data type of the MATLAB argument.

n (1,1) uint64 {mustBeReal, mustBeLessThan(n,22)}

For details, see Data Type Mappings Between C++ and Strongly Typed MATLAB Code.

stronglyTypedFactorialv2.hpp

#include "MatlabTypesInterface.hpp"

matlab::data::Array stronglyTypedFactorial(std::shared_ptr<MATLABControllerType> _matlabPtr, uint64_t n) { 
    matlab::data::ArrayFactory _arrayFactory;
    matlab::data::ArrayDimensions _dims1 = {1, 1};
    std::vector<matlab::data::Array> _args = {
        _arrayFactory.createArray<uint64_t>(_dims1, {n}) };
    matlab::data::Array _result = _matlabPtr->feval(u"stronglyTypedFactorial", _args);
    return _result;
}

Integrate C++ MATLAB Data API Shared Library Header with C++ Application

Create a C++ application code file named factApp.cpp with the following code.

factApp.cpp

// Include header files
#include "MatlabCppSharedLib.hpp"
#include "output/cpp/v2/generic_interface/stronglyTypedFactorialv2.hpp"
#include <iostream>

int main(const int argc, char *argv[])
{
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << " <path to .ctf file.>\n";
        return 0;
    }

    try {
        // Initialize MATLAB application
        auto mode = matlab::cpplib::MATLABApplicationMode::IN_PROCESS;
        std::vector<std::u16string> OPTIONS = {u"-nojvm"};
        auto appPtr = matlab::cpplib::initMATLABApplication(mode, OPTIONS);

        // Initialize MATLAB Runtime from the .ctf file
        std::string ctfName(argv[1]);
        auto libPtr = matlab::cpplib::initMATLABLibrary(appPtr, std::u16string(ctfName.cbegin(), ctfName.cend()));
        std::shared_ptr<MATLABControllerType> matlabPtr(std::move(libPtr));

        // Call the stronglyTypedFactorial MATLAB function
        matlab::data::TypedArray<uint64_t> output = stronglyTypedFactorial(matlabPtr, 5);
        std::cout << "Factorial of 5 is " << output[0] << "\n";
        appPtr.reset();
        return 0;
    } catch (const std::exception & exc) {
        std::cout << exc.what() << std::endl;
        return 1;
    }
}

Note

When writing C++ application code, you must include the header file (.hpp file) generated by the mcc command or the C++ Shared Library Compiler app and theMatlabCppSharedLib.hpp header file using#include directives.

Compile and link the C++ application at the MATLAB command prompt.

mbuild factApp.cpp -outdir output/cpp

Run the application from the system command prompt by passing the deployable archive (.ctf file) as an input. For testing purposes, you can run the application from the MATLAB command prompt.

!output\cpp\factApp.exe output\cpp\v2\generic_interface\stronglyTypedFactorial.ctf

See Also

arguments | properties

Topics