プログラムによる MATLAB Function ブロックの設定
MATLAB Function ブロックのプロパティは、プログラムを使用して調整および検査できます。プログラムを使用して MATLAB® Function レポートの内容にアクセスすることもできます。
MATLAB Function オブジェクトの呼び出し
モデルに MATLAB Function ブロックを追加した後、次のオブジェクトを使用してブロックを設定できます。
MATLABFunctionConfigurationオブジェクトを使用して、ブロックのプロパティについてクエリおよび変更を行います。ブロック パスを使用するか、関数gcbを呼び出して、ブロックを特定します。Stateflow.EMChartオブジェクトを使用して、ブロックの入力、出力、およびプロパティにアクセスします。Stateflow® API オブジェクトの階層を移動して、ブロックを特定します。
プログラムによるブロック プロパティの設定
Simulink での MATLAB Function ブロックを使用した MATLAB 関数の実装のモデル call_stats_block2 について考えます。関数 get_param を呼び出して、このモデル内の MATLAB Function ブロックに対する MATLABFunctionConfiguration オブジェクトにアクセスできます。
config = get_param("call_stats_block2/MATLAB Function", ... "MATLABFunctionConfiguration");
プロパティのクエリまたは変更を実行するには、オブジェクト名を指定してドット表記を使用します。
config.UpdateMethod
ans = 'Inherited'
config.Description = "Calculate the mean and standard deviation for a vector of values.";ブロックの入力、出力、およびプロパティへのアクセス
MATLAB Function ブロックの入力、出力、およびプロパティを変更するには、現在のシステムの Simulink.BlockDiagram オブジェクトで関数 find (Stateflow) を呼び出して、その Stateflow.EMChart オブジェクトにアクセスします。
bd = get_param(gcs,"Object"); block = find(bd,"-isa","Stateflow.EMChart", ... Path="call_stats_block2/MATLAB Function");
プロパティのクエリまたは変更を実行するには、オブジェクト名を指定してドット表記を使用します。
block.ChartUpdate
ans = 'INHERITED'
block.Description = "Calculate the mean and standard deviation for a vector of values.";Stateflow.EMChart オブジェクトによって、MATLABFunctionConfiguration オブジェクト内では使用できない追加のプロパティにアクセスできるようになります。たとえば、ブロックの入力および出力の table を作成するには、次を入力します。
info = get([block.Inputs;block.Outputs],{"Name","Scope","Port"});
T = table(info(:,2),cell2mat(info(:,3)), ...
VariableNames = ["Scope","Port"], ...
RowNames = info(:,1));
T.Scope = categorical(T.Scope)T =
3×2 table
Scope Port
______ ____
vals Input 1
mean Output 1
stdev Output 2 プログラムからの MATLAB Function レポートへのアクセス
MATLAB Function レポートにアクセスするには、次の関数を MATLABFunctionConfiguration オブジェクトで呼び出します。
openReportは、ブロックの MATLAB Function レポートを開きます。closeReportは、ブロックの MATLAB Function レポートを閉じます。getReportは、ブロックのMATLABFunctionReportオブジェクトを返します。このオブジェクトからレポート情報をクエリするには、そのFunctionsプロパティにアクセスします。このプロパティはcoder.Functionオブジェクトの配列です。coder.Function のプロパティ (MATLAB Coder)を参照してください。
たとえば、call_stats_block2 モデルの MATLAB Function ブロック内の関数と変数をリストするカスタム レポートを作成するには、次の手順に従います。
MATLAB Function ブロックの
MATLABFunctionConfigurationオブジェクトにアクセスします。config = get_param("call_stats_block2/MATLAB Function", ... "MATLABFunctionConfiguration");
MATLAB Function ブロックの
MATLABFunctionReportオブジェクトを作成します。report = getReport(config);
レポート内の
coder.Functionオブジェクトにアクセスします。functions = report.Functions;
カスタム レポートを作成します。
for i = 1:numel(functions) fprintf("Function %s uses these variables:\n",functions(i).Name) variables = functions(i).Variables; for j = 1:numel(variables) fprintf("%d. %s -- %s\n",j,variables(j).Name,variables(j).Scope) end fprintf("\n") end
Function stats uses these variables: 1. mean -- Output 2. stdev -- Output 3. vals -- Input 4. len -- Local Function avg uses these variables: 1. mean -- Output 2. array -- Input 3. size -- Input
メモ
MATLABFunctionReport オブジェクトにはエラーおよび警告に関する情報は含まれていません。MATLAB Function ブロックのエラーおよび警告を確認するには、レポートを開くか、MATLAB Function ブロック エディターでデバッガーを使用します。詳細については、MATLAB Function ブロックのデバッグを参照してください。
参考
ブロック
関数
オブジェクト
トピック
- Stateflow API の概要 (Stateflow)