メインコンテンツ

プログラムによる MATLAB インターフェイスのパブリッシュ

R2026b

この例では、ヘッダー ファイル matrixOperations.hpp で宣言され、C++ ソース ファイル matrixOperations.cpp で定義されている C++ ライブラリに対する MATLAB® インターフェイスをプログラムによって作成する方法を示します。詳細については、C/C++ ライブラリに対する MATLAB インターフェイスをパブリッシュする方法を参照してください。

ここで示している MATLAB コマンドを publishmatrix.m などのワークフロー スクリプトに保存します。このスクリプトを変更して、アップデートや将来の機能強化に対応したり、CI (継続的インテグレーション) ワークフローに統合したりできます。

インターフェイス構成の作成

ライブラリの起動パスを設定し、clibgen.api.InterfaceConfiguration オブジェクトを作成します。

rootpath = fullfile(matlabroot,"extern","examples","cpp_interface");
icfg = clibgen.api.InterfaceConfiguration("matrix");
icfg.HeaderFiles = fullfile(rootpath,"matrixOperations.hpp");

インターフェイス定義の作成

clibgen.api.InterfaceDefinition オブジェクトを作成します。

idef = clibgen.api.InterfaceDefinition(icfg);
idef.SourceFiles = fullfile(rootpath,"matrixOperations.cpp");
idef.Libraries = fullfile(rootpath,'win64/mingw64/matrixOperations.lib');

結果を表示します。IncompleteClasses プロパティと IncompleteFunctions プロパティは、定義が必要な構成要素を示します。

idef
idef = 
  Buildable InterfaceDefinition with properties:

                 Classes: [1×1 clibgen.api.ClassDefinition]
               Functions: [1×3 clibgen.api.FunctionDefinition]
                   Enums: [1×0 clibgen.api.EnumDefinition]

           InterfaceName: "matrix"
               Libraries: "C:\Program Files\MATLAB\R2026b\matlab\extern\examples\cpp_interface\win64\mingw64\matrixOperations.lib"
             SourceFiles: "C:\Program Files\MATLAB\R2026b\matlab\extern\examples\cpp_interface\matrixOperations.cpp"

       IncompleteClasses: [1×1 clibgen.api.ClassDefinition]
     IncompleteFunctions: [1×2 clibgen.api.FunctionDefinition]

      UnsupportedClasses: [1×0 clibgen.api.UnsupportedClass]
    UnsupportedFunctions: [1×0 clibgen.api.UnsupportedFunction]

  Show all properties

不完全なクラスの検査

不完全なクラスを特定します。

idef.IncompleteClasses
ans = 
  ClassDefinition with properties:

                    CPPName: "Mat"
                 MATLABName: "clib.matrix.Mat"
       HasIncompleteMembers: true
    IsConstructibleInMATLAB: true
                   Included: true

  Show all properties

Show all properties リンクをクリックします。

                   CPPName: "Mat"
                 MATLABName: "clib.matrix.Mat"
               Constructors: [1×2 clibgen.api.ConstructorDefinition]
                    Methods: [1×4 clibgen.api.MethodDefinition]
                 Properties: [1×0 clibgen.api.PropertyDefinition]
     IncompleteConstructors: [1×0 clibgen.api.ConstructorDefinition]
          IncompleteMethods: [1×3 clibgen.api.MethodDefinition]
       IncompleteProperties: [1×0 clibgen.api.PropertyDefinition]
       HasIncompleteMembers: true
    IsConstructibleInMATLAB: true
                Description: "clib.matrix.Mat    Representation of C++ class Mat."
        DetailedDescription: ""
                   Included: true

このクラスには、IncompleteMethods プロパティに示されている 3 つの不完全なメソッドがあります。

clsDef = idef.findClass("Mat");
clsDef.IncompleteMethods
ans = 
  1×3 MethodDefinition array with properties:

    CPPName
    MATLABName
    Overloaded
    OwningClassName
    CPPSignature
    MATLABSignature
    CPPInputs
    CPPOutput
    Status
    Included

  Display as table

Display as table リンクをクリックしてメソッドを特定します。

    CPPName     MATLABName    Overloaded      Status      Included
    _________    __________    __________    __________    ________

    "setMat"     "setMat"        false       Incomplete     false  
    "getMat"     "getMat"        false       Incomplete     false  
    "copyMat"    "copyMat"       false       Incomplete     false  

setMat メソッドの検査

setMat メソッドのプロパティを表示します。

methDef = clsDef.findMethod("setMat")
methDef = 

  MethodDefinition with properties:

            CPPName: "setMat"
         MATLABName: "setMat"
         Overloaded: false
    OwningClassName: "Mat"
       CPPSignature: "void Mat::setMat(int [] src,size_t len)"
    MATLABSignature: <Define incomplete inputs to see the MATLAB signature.>
          CPPInputs: [1×2 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×0 clibgen.api.OutputArgumentDefinition]
             Status: Incomplete
           Included: false

  Show all properties

表示から、Define incomplete inputs to see the MATLAB signature を行わなければなりません。入力を検査します。

methDef.CPPInputs
ans = 

  1×2 InputArgumentDefinition array with properties:

    Name
    Position
    CPPType
    MATLABType
    Direction
    Size
    Status

  Display as table

Display as table リンクをクリックして、src 引数が不完全であることを確認します。

    Name     Position    CPPType                MATLABType                  Status  
    _____    ________    ________    _________________________________    __________

    "src"       1        "int []"    "clib.array.matrix.Int"    Incomplete
    "len"       2        "size_t"    "uint64"                             Complete  

シグネチャを表示して、メソッドが引数をどのように使用しているかを確認します。

methDef.CPPSignature
ans = 

    "void Mat::setMat(int [] src,size_t len)"

src 引数はポインターです。MATLAB は src のサイズを自動的に判別できません。src を、len 引数に指定されたサイズをもつ入力配列として定義します。

argDef = methDef.CPPInputs([methDef.CPPInputs.Name] == "src");
argDef.define(Size="len");

methDef が完全に定義されていることを確認します。

methDef
methDef = 

  MethodDefinition with properties:

            CPPName: "setMat"
         MATLABName: "setMat"
         Overloaded: false
    OwningClassName: "Mat"
       CPPSignature: "void Mat::setMat(int [] src,size_t len)"
    MATLABSignature: clib.matrix.Mat/setMat(src)
          CPPInputs: [1×2 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×0 clibgen.api.OutputArgumentDefinition]
             Status: Complete
           Included: true

  Show all properties

getMat メソッドの検査

setMat メソッドを検査する手順に従って、getMat メソッドの不足している情報を定義します。

methDef = clsDef.findMethod("getMat"); % CPPOutput is incomplete.
methDef.CPPSignature % length of output defined by input argument len. 
argDef = methDef.CPPOutput;
argDef.define(Size="len");
methDef.Status

copyMat メソッドの検査

setMat メソッドを検査する手順に従って、copyMat メソッドを検査します。引数 dest を定義します。

methDef = clsDef.findMethod("copyMat"); % CPPInputs are incomplete.
methDef.CPPInputs % argument dest needs definition.
methDef.CPPSignature % The length of dest is defined by the input argument len.
argDef = methDef.CPPInputs([methDef.CPPInputs.Name] == "dest");
argDef.define(Size="len");
methDef.Status

不完全な関数の検査

不完全な関数を特定します。

idef.IncompleteFunctions
ans = 
  1×2 FunctionDefinition array with properties:

    CPPName
    MATLABName
    Overloaded
    CPPSignature
    MATLABSignature
    CPPInputs
    CPPOutput
    Status
    Included

  Display as table

Display as table リンクをクリックして、addMat 関数および updateMatBySize 関数を表示します。

         CPPName                       MATLABName                   Overloaded      Status      Included
    _________________    _______________________________________    __________    __________    ________

    "addMat"             "clib.matrix.addMat"               false       Incomplete     false  
    "updateMatBySize"    "clib.matrix.updateMatBySize"      false       Incomplete     false  

addMat 関数の検査

addMat 関数のプロパティを表示します。

fcnDef = idef.findFunction("addMat")
fcnDef = 
  FunctionDefinition with properties:

            CPPName: "addMat"
         MATLABName: "clib.matrix.addMat"
         Overloaded: false
       CPPSignature: "int addMat(Mat const * mat)"
    MATLABSignature: <Define incomplete inputs to see the MATLAB signature.>
          CPPInputs: [1×1 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×1 clibgen.api.OutputArgumentDefinition]
             Status: Incomplete
           Included: false

  Show all properties

表示から、Define incomplete inputs to see the MATLAB signature を行わなければなりません。入力を検査します。

fcnDef.CPPInputs
ans = 
  InputArgumentDefinition with properties:

          Name: "mat"
      Position: 1
       CPPType: "Mat const *"
    MATLABType: "clib.matrix.Mat"
     Direction: input
          Size: <undefined>
        Status: Incomplete

シグネチャを表示して、関数が引数をどのように使用しているかを確認します。

fcnDef.CPPSignature
ans = "int addMat(Mat const * mat)"

この関数は 1 つの mat 引数を受け取るため、Size の値は 1 です。

argDef = fcnDef.CPPInputs([fcnDef.CPPInputs.Name] == "mat");
argDef.define(Size=1);
fcnDef
fcnDef = 
  FunctionDefinition with properties:

            CPPName: "addMat"
         MATLABName: "clib.matrix.addMat"
         Overloaded: false
       CPPSignature: "int addMat(Mat const * mat)"
    MATLABSignature: RetVal = clib.matrix.addMat(mat)
          CPPInputs: [1×1 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×1 clibgen.api.OutputArgumentDefinition]
             Status: Complete
           Included: true

  Show all properties

updateMatBySize 関数の検査

updateMatBySize 関数のプロパティを表示します。

fcnDef = idef.findFunction("updateMatBySize")
fcnDef = 
  FunctionDefinition with properties:

            CPPName: "updateMatBySize"
         MATLABName: "clib.matrix.updateMatBySize"
         Overloaded: false
       CPPSignature: "void updateMatBySize(Mat & mat,int * arr,size_t len)"
    MATLABSignature: <Define incomplete inputs to see the MATLAB signature.>
          CPPInputs: [1×3 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×0 clibgen.api.OutputArgumentDefinition]
             Status: Incomplete
           Included: false

  Show all properties

表示から、Define incomplete inputs to see the MATLAB signature を行わなければなりません。入力を検査します。

fcnDef.CPPInputs
ans = 
  1×3 InputArgumentDefinition array with properties:

    Name
    Position
    CPPType
    MATLABType
    Direction
    Size
    Status

  Display as table

Display as table リンクをクリックします。

    Name     Position    CPPType           MATLABType             Status  
    _____    ________    ________    _______________________    __________

    "mat"       1        "Mat &"     "clib.matrix.Mat"          Complete  
    "arr"       2        "int *"     "clib.array.matrix.Int"    Incomplete
    "len"       3        "size_t"    "uint64"                   Complete  

シグネチャを表示して、関数が引数をどのように使用しているかを確認します。

fcnDef.CPPSignature
ans = "void updateMatBySize(Mat & mat,int * arr,size_t len)"

長さ arr を入力引数 len で定義します。

argDef = fcnDef.CPPInputs([fcnDef.CPPInputs.Name] == "arr");
argDef.define(Size="len")
fcnDef
fcnDef = 
  FunctionDefinition with properties:

            CPPName: "updateMatBySize"
         MATLABName: "clib.matrix.updateMatBySize"
         Overloaded: false
       CPPSignature: "void updateMatBySize(Mat & mat,int * arr,size_t len)"
    MATLABSignature: clib.matrix.updateMatBySize(mat, arr)
          CPPInputs: [1×3 clibgen.api.InputArgumentDefinition]
          CPPOutput: [1×0 clibgen.api.OutputArgumentDefinition]
             Status: Complete
           Included: true

  Show all properties

インターフェイスのビルド

InterfaceDefinition build 関数を使用します。

build(idef)

インターフェイス ファイル フォルダーを MATLAB パスに追加します。

libpath = fullfile(pwd,"matrix");
addpath(libpath)

ワークフロー スクリプトの保存

publishmatrix.m スクリプトには、次のような MATLAB コードが含まれます。

%Publish MATLAB Interface to matrixOperations
%Create Interface Configuration
rootpath = fullfile(matlabroot,"extern","examples","cpp_interface");
icfg = clibgen.api.InterfaceConfiguration("matrix");
icfg.HeaderFiles = fullfile(rootpath,"matrixOperations.hpp");

%Create Interface Definition
idef = clibgen.api.InterfaceDefinition(icfg);
idef.SourceFiles = fullfile(rootpath,"matrixOperations.cpp");
idef.Libraries = fullfile(rootpath,'win64/mingw64/matrixOperations.lib');

%Define Methods in Class Mat
clsDef = idef.findClass("Mat");

methDef = clsDef.findMethod("setMat");
argDef = methDef.CPPInputs([methDef.CPPInputs.Name] == "src");
argDef.define(Size="len");

methDef = clsDef.findMethod("getMat");
argDef = methDef.CPPOutput;
argDef.define(Size="len");

methDef = clsDef.findMethod("copyMat");
argDef = methDef.CPPInputs([methDef.CPPInputs.Name] == "dest");
argDef.define(Size="len");

%Define Incomplete Functions
fcnDef = idef.findFunction("addMat");
argDef = fcnDef.CPPInputs([fcnDef.CPPInputs.Name] == "mat");
argDef.define(Size=1);

fcnDef = idef.findFunction("updateMatBySize");
argDef = fcnDef.CPPInputs([fcnDef.CPPInputs.Name] == "arr");
argDef.define(Size="len");

%Build Interface
build(idef)
libpath = fullfile(pwd,"matrix");
addpath(libpath)

インターフェイスのテスト

アウトプロセス実行モードの有効化

定義ファイルを変更する必要がある場合は、このコマンドを実行して、アウトプロセスでインターフェイス ライブラリを呼び出す機能を設定し、MATLAB を再起動しなくて済むようにします。詳細については、インプロセスまたはアウトプロセスでの C/C++ ライブラリの読み込みを参照してください。

libraryconfig = clibConfiguration("matrix",ExecutionMode="outofprocess")

テスト コードの記述

インターフェイス ライブラリを呼び出してテストするコードを記述します。

matObj = clib.matrix.Mat;   % Create a Mat object
intArr = [1,2,3,4,5];
matObj.setMat(intArr);     % Set the values to intArr
retMat = matObj.getMat(5)  % Display the values
retMat = 

  read-only clib.array.matrix.Int with properties:

    Dimensions: 5
     Resizable: 0

インターフェイスの共有

インターフェイスを別の MATLAB ユーザーに共有するには、パッケージ インストール (.mltbx) ファイルを作成します。Distribute MATLAB Interface to C/C++ Libraryの手順を使用して以下を実行します。

  • パッケージ フォルダーを、インターフェイス ファイル matrixInterface.dll が含まれている matrix フォルダーに設定します。

  • 同じフォルダー内に、コンパイル済みライブラリ ファイル matrix.dll を配置します。

  • 名前空間 (呼び出し構文) を clib.matrix として識別します。

参考

オブジェクト

関数

トピック