このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
複数の MATLAB 関数を使用した Python アプリケーションの作成
サポートされるプラットフォーム: Windows®、Linux®、Mac
この例では、複数の MATLAB® 関数を使用して四角形のデータを計算する Python® アプリケーションを作成する方法を説明します。
前提条件
MATLAB Compiler SDK™ と互換性のあるバージョンの Python がインストールされていることを確認します。詳細については、MATLAB でサポートされている他言語のインターフェイスを参照してください。
この例では、
にある以下のファイルを使用します。
\extern\examples\compilersdk\python\rectangle\matlabroot
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 アプリケーションの作成と実行
MATLAB コマンド プロンプトで、MATLAB に付属している
rectangle
フォルダーを作業フォルダーにコピーします。copyfile(fullfile(matlabroot,"extern","examples","compilersdk","python","rectangle"),"rectangle");
作業フォルダー内の新しい
rectangle
フォルダーに移動します。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
rectangle
フォルダー内のすべての MATLAB 関数ファイルを使用して、rectangleLib
という名前の Python パッケージをビルドします。現在のディレクトリにある、拡張子が
.m
のファイルのリストを取得します。functionfiles = dir('*.m')
ファイル名を cell 配列に保存します。
functionfiles = {functionfiles.name};
compiler.build.pythonPackage
を使用して Python パッケージをコンパイルします。buildResults = compiler.build.pythonPackage(functionfiles,'PackageName','rectangleLib');
詳細については、MATLAB 関数のパッケージ化と Python アプリケーションへのデプロイを参照してください。
MATLAB 関数にアクセスする Python アプリケーションのソース コードを作成します。
この例のサンプル アプリケーションは
rectangle
フォルダー内のrectangleDriver.py
です。rectangleDriver
アプリケーションでは、以下を行います。2 つのクラス
PyPoint
およびPyRectangle
を定義するrectangleLib.initialize
を使用してパッケージへのハンドルを作成するパッケージ ハンドルを
PyPoint
クラスおよびPyRectangle
クラスに渡すPyRectangle
オブジェクトを作成し、その面積、高さ、および幅を出力する各頂点を
PyPoint
オブジェクトとして保存し、その座標を出力するMATLAB 関数
rectangleDemo
を呼び出し、異なる四角形のデータを作成して表示する
システム コマンド プロンプト ウィンドウを開き、生成されたパッケージが含まれている
rectangleLibpythonPackage
フォルダーに移動します。python
コマンドを使用してパッケージをインストールします。python setup.py install
詳細については、MATLAB Compiler SDK Python パッケージのインストールとインポートを参照してください。
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
/binmatlabroot
は MATLAB または MATLAB Runtime がインストールされている場所です。
参考
mwpython
| compiler.build.pythonPackage