永続変数のコード生成
この例では、永続変数を使用する MATLAB® 関数 compute_average から MEX 関数を生成する方法を示します。また、関数を使って新しい値セットの平均を計算する前に、永続変数の状態をクリアしなければならないということも示します。
この例では、スタンドアロンの生成コードで同じ MATLAB 関数の永続変数の状態を初期化および終了する方法も説明します。関数を使って新しい値セットの平均を計算する前に、生成コード内の永続変数の状態をクリアしなければなりません。
前提条件
この例には前提条件はありません。
関数 compute_average について
関数 compute_average.m は、一度に 1 つの値で関数を呼び出せるように、2 つの永続変数 (累積合計と、これまで追加された値の数) を使用します。
type compute_average% y = compute_average(x)
% This function takes an input scalar value 'x' and returns the average
% value so far.
function y = compute_average(x) %#codegen
assert(isa(x,'double')); % Input is scalar double
% Declare two persistent variables 'sum' and 'cnt'.
persistent sum cnt;
% Upon the first call we need to initialize the variables.
if isempty(sum)
sum = 0;
cnt = 0;
end
% Compute the accumulated sum and the number of values so far.
sum = sum + x;
cnt = cnt + 1;
% Return the current average.
y = sum / cnt;
%#codegen 命令は、当該の MATLAB コードがコード生成用であることを示します。
MEX 関数の生成
最初に、コマンドcodegenを使用して MEX 関数を生成し、コンパイルする MATLAB ファイルの名前を指定します。入力引数の型を double 型のスカラーに指定します。
codegen compute_average -args 0
Code generation successful.
既定で、codegen は、現在のフォルダーに compute_average_mex という名前の MEX 関数を生成します。これにより、MATLAB コードと MEX 関数をテストして結果を比較することができます。
MEX 関数の実行
(10 + 20 + 100) / 3 = 43.3333
compute_average_mex(10)
ans = 10
compute_average_mex(20)
ans = 15
compute_average_mex(100)
ans = 43.3333
永続変数の内部状態のクリア
clear mex コマンドを使って永続変数をクリアします。
clear mexMEX 関数を再度実行して異なる値セットの平均を計算
(10 + 20 + 30 + 40) / 4 = 25
compute_average_mex(10)
ans = 10
compute_average_mex(20)
ans = 15
compute_average_mex(30)
ans = 20
compute_average_mex(40)
ans = 25
スタンドアロンの生成コードに含まれる永続変数の内部状態のクリア
スタンドアロンの生成コードに含まれる永続変数の状態をクリアするには、main 関数で初期化関数と終了関数を呼び出します。これらの関数はコード ジェネレーターによって生成されます。これらのファイルは、codegen ディレクトリ内にあります。
main ファイルの例 main.c を編集して、初期化関数と終了関数を呼び出すことができます。以下に例を示します。
type main.c/*
* File: main.c
*/
/* Include Files */
#include "main.h"
#include "compute_average.h"
#include "compute_average_terminate.h"
#include "compute_average_initialize.h"
/* Function Declarations */
static double argInit_real_T(void);
static void main_compute_average(void);
/* Function Definitions */
/*
* Arguments : void
* Return Type : double
*/
static double argInit_real_T(void)
{
return 0.0;
}
/*
* Arguments : void
* Return Type : void
*/
static void main_compute_average(void)
{
double y;
/* Initialize function 'compute_average' input arguments. */
/* Call the entry-point 'compute_average'. */
y = compute_average(argInit_real_T());
}
/*
* Arguments : int argc
* const char * const argv[]
* Return Type : int
*/
int main(int argc, const char * const argv[])
{
(void)argc;
(void)argv;
/* Initialize the entry-point function. */
compute_average_initialize();
/* Invoke the entry-point functions.
You can call entry-point functions multiple times. */
main_compute_average();
/* Terminate the application. */
compute_average_terminate();
/*Once the application is terminated, the state of the persistent variables is cleared. */
/* Re-initialize the entry-point function. */
compute_average_initialize();
/* You can run the application for a new set of values.*/
main_compute_average();
/* Terminate the application after your process is complete.*/
compute_average_terminate();
return 0;
}
/*
* File trailer for main.c
*
* [EOF]
*/
ここに示すように、main.c ファイルは、終了関数 compute_average_terminate() を呼び出して永続変数の状態をクリアするように編集されています。新しい計算セットは、新しい値セットを使用して compute_average_initialize() と main_compute_average() を呼び出すことで実行されます。