Main Content

複数の MATLAB 関数を使用した Python アプリケーションの作成

サポートされるプラットフォーム: Windows®、Linux®Mac

この例では、複数の MATLAB® 関数を使用して四角形のデータを計算する Python® アプリケーションを作成する方法を説明します。

前提条件

MATLAB Compiler SDK™ と互換性のあるバージョンの Python がインストールされていることを確認します。詳細については、MATLAB でサポートされている他言語のインターフェイスを参照してください。

この例では、matlabroot\extern\examples\compilersdk\python\rectangle\ にある以下のファイルを使用します。

MATLAB 関数getPointCoordinates.m
getRectangleArea.m
getRectangleCorners.m
getRectangleHeight.m
getRectangleWidth.m
makePoint.m
makeRectangle.m
Point.m
Rectangle.m
rectangleDemo.m
Python アプリケーション コードrectangleDriver.py

Python アプリケーションの作成と実行

  1. MATLAB コマンド プロンプトで、MATLAB に付属している rectangle フォルダーを作業フォルダーにコピーします。

    copyfile(fullfile(matlabroot,"extern","examples","compilersdk","python","rectangle"),"rectangle");

    作業フォルダー内の新しい rectangle フォルダーに移動します。

  2. MATLAB 関数 rectangleDemo.m を確認します。

    この関数により、2 つの Point オブジェクトが作成されてから、各点を頂点として使用して Rectangle オブジェクトが作成されます。その後、四角形に関するデータが計算されて表示されます。

    function rectangleDemo()
        % RECTANGLEDEMO Construct a rectangle and print information about it
    
        pointZeroZero = makePoint(0, 0);
        pointThreeFour = makePoint(3, 4);
        rectA = makeRectangle(pointZeroZero, pointThreeFour);
        corners = getRectangleCorners(rectA);
        showPointCoordinates(corners.upperLeft, 'Upper left-hand corner');
        showPointCoordinates(corners.lowerLeft, 'Lower left-hand corner');
        showPointCoordinates(corners.upperRight, 'Upper right-hand corner');
        showPointCoordinates(corners.lowerRight, 'Lower right-hand corner');
        fprintf('Area: %.1f\n', area(rectA));
        fprintf('Height: %.1f\n', height(rectA));
        fprintf('Width: %.1f\n', width(rectA));
    end
    
    % This is an auxiliary function. It cannot be called outside rectangleDemo().
    function showPointCoordinates(pt, desc)
        coordinates = getPointCoordinates(pt);
        fprintf('%s: (%.1f, %.1f)\n', desc, coordinates.X, coordinates.Y);
    end
    

  3. rectangle フォルダー内のすべての MATLAB 関数ファイルを使用して、rectangleLib という名前の Python パッケージをビルドします。

    1. 現在のディレクトリにある、拡張子が .m のファイルのリストを取得します。

      functionfiles = dir('*.m')

    2. ファイル名を cell 配列に保存します。

      functionfiles = {functionfiles.name};

    3. compiler.build.pythonPackage を使用して Python パッケージをコンパイルします。

      buildResults = compiler.build.pythonPackage(functionfiles,'PackageName','rectangleLib');

      詳細については、MATLAB 関数のパッケージ化と Python アプリケーションへのデプロイを参照してください。

  4. MATLAB 関数にアクセスする Python アプリケーションのソース コードを作成します。

    この例のサンプル アプリケーションは rectangle フォルダー内の rectangleDriver.py です。

     rectangleDriver.py

    rectangleDriver アプリケーションでは、以下を行います。

    1. 2 つのクラス PyPoint および PyRectangle を定義する

    2. rectangleLib.initialize を使用してパッケージへのハンドルを作成する

    3. パッケージ ハンドルを PyPoint クラスおよび PyRectangle クラスに渡す

    4. PyRectangle オブジェクトを作成し、その面積、高さ、および幅を出力する

    5. 各頂点を PyPoint オブジェクトとして保存し、その座標を出力する

    6. MATLAB 関数 rectangleDemo を呼び出し、異なる四角形のデータを作成して表示する

  5. システム コマンド プロンプト ウィンドウを開き、生成されたパッケージが含まれている rectangleLibpythonPackage フォルダーに移動します。

  6. python コマンドを使用してパッケージをインストールします。

    python setup.py install

    詳細については、MATLAB Compiler SDK Python パッケージのインストールとインポートを参照してください。

  7. 1 つ上のディレクトリに移動し、rectangleDriver.py アプリケーションを実行します。

    cd .. python rectangleDriver.py

    Initializing module and creating a rectangle with corners (-1, 2) and (5, 10)...
    Area: 48.0
    Height: 8.0
    Width: 6.0
    Corners:
        upperLeft: (-1.0, 2.0)
        lowerLeft: (-1.0, 10.0)
        upperRight: (5.0, 2.0)
        lowerRight: (5.0, 10.0)
    
    Executing rectangleDemo...
    Upper left-hand corner: (0.0, 0.0)
    Lower left-hand corner: (0.0, 4.0)
    Upper right-hand corner: (3.0, 0.0)
    Lower right-hand corner: (3.0, 4.0)
    Area: 12.0
    Height: 4.0
    Width: 3.0
    

    メモ

    macOS では、python の代わりに mwpython スクリプトを使用する必要があります。たとえば、mwpython rectangleDriver.py です。

    mwpython スクリプトは matlabroot/bin フォルダーにあります。ここで、matlabroot は MATLAB または MATLAB Runtime がインストールされている場所です。

参考

|

関連するトピック