Convert C++ Engine Application to MATLAB Compiler SDK Application - MATLAB & Simulink (original) (raw)

Main Content

You can deploy an engine application by modifying your code to use the MATLAB® Compiler SDK™ API. For information about this API, see Deploy to C++ Applications Using MATLAB Data API (C++11) (MATLAB Compiler SDK). Make these modifications to use the equivalent Compiler SDK C++ values:

Applications to Call Sierpinski Function

This example is an engine application that calls a MATLAB function sierpinski which calculates the points in Sierpinski's triangle. The example shows how to modify the engine code to run withMATLAB Compiler SDK.

If you have Compiler SDK, then you can use the graphical Sierpinski function sierpinski.m in the_`matlabroot`_/extern/examples/compilersdk/c_cpp/triangle folder.

Copy this code into the file triangleEngine.cpp.

Engine Application triangleEngine.cpp

/*============================================================== *

#include "MatlabEngine.hpp" #include

namespace mc = matlab::engine; namespace md = matlab::data;

const int DEFAULT_NUMBER_OF_POINTS = 1000; std::u16string convertAsciiToUtf16(const std::string & asciiStr);

int mainFunc(std::shared_ptrmc::MATLABEngine app, const int argc, const char * argv[]) { try { size_t numPoints(DEFAULT_NUMBER_OF_POINTS); if(argc > 1) { numPoints = std::stoi(argv[1]); }

    // Create arguments as input of the MATLAB function
    md::ArrayFactory factory;
    matlab::data::TypedArray<size_t> numPointsAsArray = factory.createScalar(numPoints);
    matlab::data::TypedArray<bool> doDrawAsArray = factory.createScalar(doDraw); // create a logical scalar
    std::vector<md::Array> inputs{numPointsAsArray, doDrawAsArray};

    // The Sierpinski function returns the X and Y coordinates of the points
    // in the triangle. If doDraw is true, it also draws the figure.
    app->feval(u"sierpinski", 2, inputs);

    // Wait until the user closes the figure.
    app->eval(u"figures = findobj(allchild(groot),'flat','type','figure','visible','on'); 
        waitfor(figures(1))");
            
} catch(std::exception & exc) {
    std::cerr << exc.what() << std::endl;
    return -1;
}
return 0;

}

int main(const int argc, const char * argv[]) { int ret = 0; try {

    std::vector<std::u16string> options = {};
    std::unique_ptr<MATLABEngine> matlabApplication = mc::connectMATLAB();//startMATLAB();
    ret = mainFunc(std::move(matlabApplication), argc, argv);
} catch(const std::exception & exc) {
    std::cerr << exc.what() << std::endl;
    return -1;
}
return ret;

}

Modify the code to use the equivalent Compiler SDK C++ values.

If you copy the following code into triangleSDK.cpp and compare the file with triangleEngine.cpp, then you can see other modifications relevant to calling the Sierpinski function.

Corresponding Deployed Application triangleSDK.cpp

/*============================================================== *

#include "MatlabCppSharedLib.hpp" #include #include

namespace mc = matlab::cpplib; namespace md = matlab::data;

const int DEFAULT_NUMBER_OF_POINTS = 1000;

int mainFunc(std::shared_ptrmc::MATLABApplication app, const int argc, const char * argv[]) { try { size_t numPoints(DEFAULT_NUMBER_OF_POINTS); if(argc > 1) { numPoints = std::stoi(argv[1]); } // Pass the path of the CTF (library archive file) to initMATLABL ibrary const std::u16string U16STR_CTF_NAME = u"libtriangle.ctf"; auto lib = mc::initMATLABLibrary(app, U16STR_CTF_NAME);

    // Create arguments as input of the MATLAB functio
    md::ArrayFactory factory;
    matlab::data::TypedArray<size_t>. numPointsAsArray = factory.createScalar(numPoints);
    matlab::data::TypedArray<bool> doDrawAsArray = factory.createScalar(doDraw); // create a logical scalar
    std::vector<md::Array> inputs{numPointsAsArray, doDrawAsArray};

    // The Sierpinski function returns the X and Y coordinates of the points
    // in the triangle. If doDraw is true, it also draws the figure.
    lib->feval(u"sierpinski", 2, inputs);

    // Wait until the user closes the figure.
    lib->waitForFiguresToClose();

    
} catch(std::exception & exc) {
    std::cerr << exc.what() << std::endl;
    return -1;
}
return 0;

}

int main(const int argc, const char * argv[]) { int ret = 0; try { matlab::cpplib::MATLABApplicationMode mode = mc::MATLABApplicationMode::IN_PROCESS; std::vectorstd::u16string options = {}; std::shared_ptrmatlab::cpplib::MATLABApplication matlabApplication = mc::initMATLABApplication(mode, options);

    ret = mc::runMain(mainFunc, std::move(matlabApplication), argc, argv);
} catch(const std::exception & exc) {
    std::cerr << exc.what() << std::endl;
    return -1;
}
return ret;

}

See Also

Topics