Timer Callback Functions

Note

如果回调涉及CPU密集型任务,例如更新图形,则可以延迟回调功能执行。

Associating Commands with Timer Object Events

这计时器object supports properties that let you specify the MATLAB®命令在计时器启动时执行该执行,并为其他计时对象事件(例如启动,停止或发生错误时)执行。这些被称为callbacks。要将MATLAB命令与计时器对象事件相关联,请设置关联的计时器对象回调属性的值。

这following diagram shows when the events occur during execution of a timer object and give the names of the timer object properties associated with each event. For example, to associate MATLAB commands with a start event, assign a value to theStartfcn回调属性。错误回调可能随时发生。

Timer Object Events and Related Callback Function

创建回调函数

When the time period specified by a timer object elapses, the timer object executes one or more MATLAB functions of your choosing. You can specify the functions directly as the value of the callback property. You can also put the commands in a function file and specify the function as the value of the callback property.

Specifying Callback Functions Directly

此示例创建一个计时器对象,该对象在5秒后显示问候。该示例指定了TIMERFCN直接回调属性,将命令放在字符向量中。

t = timer('timerfcn',@(x,y)disp('Hello world!'),'startDelay',5);

Note

When you specify the callback commands directly as the value of the callback function property, the commands are evaluated in the MATLAB workspace.

Putting Commands in a Callback Function

Instead of specifying MATLAB commands directly as the value of a callback property, you can put the commands in a MATLAB program file and specify the file as the value of the callback property.

当您创建一个回调函数,前两个arguments must be a handle to the timer object and an event structure. An event structure contains two fields:类型数据。这类型字段包含一个字符向量,该角色向量识别导致回调的事件类型。该字段的价值可以是以下任何一个:'startfcn','StopFcn','TimerFcn', or'ErrorFcn'。这数据field contains the time the event occurred.

除了这两个所需的输入参数外,您的回调函数还可以接受特定于应用程序的参数。要接收这些输入参数,在将函数名称指定为回调属性的值时,必须使用单元格数组。有关更多信息,请参阅指定回调函数属性的值

Example: Writing a Callback Function

This example implements a simple callback function that displays the type of event that triggered the callback and the time the callback occurred. To illustrate passing application-specific arguments, the example callback function accepts as an additional argument a character vector and includes this text in the display output. To see this function used with a callback property, see指定回调函数属性的值

function my_callback_fcn(obj, event, text_arg) txt1 = ' event occurred at '; txt2 = text_arg; event_type = event.Type; event_time = datestr(event.Data.time); msg = [event_type txt1 event_time]; disp(msg) disp(txt2)

指定回调函数属性的值

You associate a callback function with a specific event by setting the value of the appropriate callback property. You can specify the callback function as a cell array or function handle. If your callback function accepts additional arguments, you must use a cell array.

下表显示了几个示例回调函数的语法,并描述了您如何调用它们。

Callback Function Syntax

How to Specify as a Property Value for Objectt

功能myfile(obj,event)

t.startfcn = @myfile

功能myfile

t.StartFcn = @(~,~)myfile

功能myfile(obj, event, arg1, arg2)

t.startfcn = {@myfile,5,6}

This example illustrates several ways you can specify the value of timer object callback function properties, some with arguments and some without. To see the code of the callback function,my_callback_fcn, seeExample: Writing a Callback Function:

  1. Create a timer object.

    t = timer('StartDelay', 4, 'Period', 4, 'TasksToExecute', 2, ... 'ExecutionMode', 'fixedRate');
  2. Specify the value of theStartfcn打回来。请注意,该示例指定单元格数组中的值,因为回调函数需要访问传递给它的参数:

    t.startfcn = {@my_callback_fcn,'我的开始消息'};
  3. Specify the value of theStopFcn打回来。Again, the value is specified in a cell array because the callback function needs to access the arguments passed to it:

    t.StopFcn = { @my_callback_fcn, 'My stop message'};
  4. Specify the value of theTIMERFCN打回来。这example specifies the MATLAB commands in a character vector:

    t.TimerFcn = @(x,y)disp('Hello World!');
  5. 启动计时器对象:

    start(t)

    这example outputs the following.

    StartFCN事件发生在2004年3月10日17:16:59我的开始消息Hello World!你好世界!StopFCN事件发生在2004年3月10日17:16:59我的停止消息
  6. Delete the timer object after you are finished with it.

    delete(t)

See Also

相关话题