为持久变量生成代码
此示例说明如何从使用持久变量的 MATLAB® 函数 compute_average 生成 MEX 函数。它说明在使用该函数计算新值集的平均值之前,必须清除持久变量的状态。
此示例还说明如何在独立生成代码中初始化和终止同一 MATLAB 函数的持久变量状态。在使用该函数计算新值集的平均值之前,您必须清除生成代码中持久变量的状态。
前提条件
此示例没有任何前提条件。
关于 compute_average 函数
compute_average.m 函数使用两个持久变量,即累积和与迄今为止添加的值数目,以便您可以一次使用一个值调用该函数。
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 后跟要编译的 MATLAB 文件的名称,生成 MEX 函数。指定输入参量的类型为标量双精度。
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 mex再次运行 MEX 函数以计算不同值集的平均值
(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
清除独立生成代码中持久变量的内部状态
独立生成代码中持久变量的状态通过在主函数中调用初始化和终止函数来清除。这些函数由代码生成器生成。这些文件位于 codegen 目录中。
您可以编辑示例主文件 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() 来运行一组新计算。