メインコンテンツ

C++ エンジン アプリケーションを MATLAB Compiler SDK アプリケーションに変換

R2026b

MATLAB® Compiler SDK™ API を使用するようにコードを変更することで、エンジン アプリケーションを展開することができます。この API の詳細については、MATLAB データ API を使用した C++ アプリケーションへのデプロイ (C++11) (MATLAB Compiler SDK)を参照してください。同等の Compiler SDK C++ 値を使用するには、次のように変更します。

  • ヘッダー ファイル

    MatlabCppSharedLib.hpp

  • 名前空間

    matlab::cpplib

  • クラス名

    matlab::cpplib::MATLABApplication

  • Compiler SDK では CTF (ライブラリ アーカイブ ファイル) のパスを設定する必要があります。以下に例を示します。

    const std::u16string U16STR_CTF_NAME = u"libtriangle.ctf";
    auto lib = mc::initMatlabLibrary(app, U16STR_CTF_NAME);

Sierpinski 関数を呼び出すアプリケーション

この例は、Sierpinski の三角形の点を計算する MATLAB 関数 sierpinski を呼び出すエンジン アプリケーションです。例では、MATLAB Compiler SDK で実行されるようにエンジン コードを変更する方法を説明します。

Compiler SDK がある場合は、matlabroot/extern/examples/compilersdk/c_cpp/triangle フォルダー内のグラフィカルな Sierpinski 関数 sierpinski.m を使用することができます。

このコードをファイル triangleEngine.cpp にコピーします。

/*==============================================================
 *
 * triangleEngine.cpp
 * 
 * MATLAB Engine API for C++ application
 *============================================================*/

#include "MatlabEngine.hpp"
#include <iostream>

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_ptr<mc::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;
}

同等の Compiler SDK C++ 値を使用するようにコードを変更します。

次のコードを triangleSDK.cpp にコピーして、ファイルを triangleEngine.cpp と比較すると、Sierpinski 関数の呼び出しに関連するその他の変更を確認できます。

/*==============================================================
 *
 * triangleSDK.cpp
 * Use the generic interface for the C++ shared library
 * generated by MATLAB Compiler SDK.
 *============================================================*/

#include "MatlabCppSharedLib.hpp"
#include <iomanip>
#include <iostream>

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

const int DEFAULT_NUMBER_OF_POINTS = 1000;


int mainFunc(std::shared_ptr<mc::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::vector<std::u16string> options = {};
        std::shared_ptr<matlab::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;
}

参考

トピック