필터 지우기
필터 지우기

Using a class method as timer callback

조회 수: 7 (최근 30일)
Murali
Murali 2024년 5월 24일
댓글: Yatharth 2024년 5월 24일
Hello,
I am trying to use a matlab app designer class method as a timer callback.
Class definition.
classdef S1_Bluetooth_GUI < matlab.apps.AppBase
I have a timer object called 't' as a class property.
properties (Access = private)
t;
end
This is the timer callback method in a gist.
methods (Access = private)
function execAlive(~,~,app)
% Do stuff with 'app', mostly accessing class properties....
end
end
This is the method I am using to register the timer callback. This bit of code is in another member callback that handle component events
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
app.t = timer('Period', 2 , 'ExecutionMode', 'fixedRate');
app.t.TimerFcn = {@execAlive,app}; % Register timer callback function
end
end
When I execute this, I am getting the error below
Error while evaluating TimerFcn for timer 'timer-6'
Undefined function 'execAlive' for input arguments of type 'timer'.
Not sure what I am doing wrong here, somehow the timer callback isn`t working. I have tried the same by putting the callback in a separate script file and that works, but when I try to add it as a class method it doesn`t. Any help would be much appreciated!

채택된 답변

Yatharth
Yatharth 2024년 5월 24일
Hi Murali,
In the context of your AppDesigner class, the execAlive method is defined with the class instance (app) as its third argument. However, when setting up the timer callback, MATLAB expects the first input to the callback function to be the source of the event (in this case, the timer object t) and the second input to be the event data, which is usually empty ([]) for timer callbacks. Your execAlive method is defined to ignore the first two arguments and expects the third argument to be the app instance itself, which is correct for how you're intending to use it.
To resolve this issue, you should use the method within the context of the class instance (app). Here's how you can adjust your timer callback registration to correctly reference the execAlive method within the class:
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
app.t = timer('Period', 2 , 'ExecutionMode', 'fixedRate');
% Adjust the TimerFcn to use a function handle that calls execAlive on app
app.t.TimerFcn = @(src, event)execAlive(app, src, event); % Correctly reference execAlive
start(app.t); % Don't forget to start the timer
end
  댓글 수: 2
Murali
Murali 2024년 5월 24일
That worked!, thanks very much :). I come from C++ and callbacks work a bit differently there.
Yatharth
Yatharth 2024년 5월 24일
No worries, it would be great if you accept this answer so that it's reach increases in case of similar queries!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by