メインコンテンツ

Simulink での MATLAB Function ブロックを使用した MATLAB 関数の実装

MATLAB Functionブロックでは、MATLAB® コードを使用して Simulink® モデル内にカスタム関数を定義できます。これらのブロックは次のような場合に使用します。

  • カスタム機能をモデル化する既存の MATLAB 関数がある場合。または、こうした関数を容易に作成できる場合。

  • Simulink グラフィカル言語では取得されない、または取得できないカスタム機能がモデルに必要な場合。

  • Simulink ブロック線図を使うより MATLAB 関数を使う方がカスタム機能のモデル化が容易な場合。

  • モデル化するカスタム機能に連続状態または離散ダイナミクス状態が含まれない場合。動的状態をモデル化するには、S-Function を使用する。MATLAB S-Function を使用したブロックの作成を参照してください。

MATLAB Function ブロックによる平均と標準偏差の計算

この例は MATLAB Function ブロックを含むモデルから始まり、値のベクトルの平均と標準偏差を計算するためにブロックをカスタマイズする方法を順を追って説明していきます。call_stats_block1 モデルを開いて開始時のブロックを確認します。モデルには、空の MATLAB Function ブロックとこの例に必要なブロックが含まれています。

The starting model. The model contains a MATLAB Function block in the center, a Constant block that has a vector with four values to the left, and two Display blocks to the right. The blocks are not connected.

MATLAB Function ブロックのプログラム

ベクトルの値から平均と標準偏差を計算するブロックをプログラムします。MATLAB Function ブロック エディターを開くには、MATLAB Function ブロックをダブルクリックします。MATLAB Function ブロック エディターに、2 つの変数 (1 つの入力引数と 1 つの出力引数) をもつ既定の関数が表示されます。

This image shows the MATLAB Function Block Editor after it has been opened from the call_stats_block1 model. It includes a default function.

関数宣言ステートメントを編集して関数の入力と出力を定義します。

function [mean, stdev] = stats(vals)

このステートメントは、stats という名前の、3 つの変数をもつ関数を定義します。このステートメントは、1 つの入力引数 vals と、2 つの出力引数 mean および stdev を定義します。

関数宣言ステートメントの後の新しい行で、既存のコードを消去して以下のコードを追加します。

% Calculates a statistical mean and a standard
% deviation for the values in vals.

len = length(vals);
mean = avg(vals,len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
plot(vals,"-+");

function mean = avg(array,size)
mean = sum(array)/size;

最終的なコードは次のようになります。

The MATLAB Function Block Editor showing the final version of the code. It includes the code from the previous steps.

ブロックを終了します。ブロックにより端子名が更新されます。関数の出力 mean および stdev はブロックの出力端子 "mean" および "stdev" に対応し、関数の入力 vals はブロックの入力端子 "vals" に対応します。

This shows the outline of the block without connections between the blocks.

MATLAB Function ブロックへの接続を完成させます。

This shows the connected signals between the blocks established in the previous image.

モデルを保存します。このモデルの完成したバージョンを確認するには、call_stats_block2 モデルを開きます。

MATLAB Function ブロック変数のプロパティのチェック

MATLAB Function ブロック内の変数のプロパティをチェックして管理できます。この例では、入力引数 vals がその型とサイズのプロパティを継承していることを検証します。MATLAB Function ブロックをダブルクリックします。[シンボル] ペインとプロパティ インスペクターを開きます。[関数] タブで、[データの編集] をクリックします。

The Function tab in the Simulink Editor while the MATLAB Function block Editor is open. The Edit Data button on the left is enclosed in a red box.

[シンボル] ペインの [名前] 列に変数名が表示されます。[タイプ] 列により、関数の引数の端子の割り当てと、関数宣言ステートメントのどこに表示されるかが決まります。この例では、vals は入力端子に割り当てられます。meanstdev は出力端子に割り当てられます。いずれかの変数の [タイプ] エントリを変更すると、関数宣言ステートメントも変化します。

vals のプロパティをチェックします。[シンボル] ペインで、vals の行をクリックします。プロパティ インスペクターが更新され、vals のプロパティが表示されます。[スコープ] プロパティは [タイプ] 列に一致しています。

The Property Inspector and the Symbols pane. The vals input variable is selected, and the Property Inspector displays the variable properties.

MATLAB Function ブロックでの変数の作成とプロパティの設定の詳細については、MATLAB Function ブロックの変数の作成と定義を参照してください。

モデルのシミュレーション

Simulink で [実行] をクリックしてモデルをシミュレートします。モデルは関数のデータを 2 つの Display ブロックに出力します。

The Display blocks show the simulation results of the model. The first Display block shows 3.5 and the second shows 1.118.

さらに、ブロックが別のウィンドウに入力データをプロットします。

A plot of the input values and a line drawn through the values. The line is blue.

参考

| |

トピック

外部の Web サイト