Clear Filters
Clear Filters

Using a class method as timer callback

7 views (last 30 days)
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!

Accepted Answer

Yatharth
Yatharth on 24 May 2024
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 Comments
Murali
Murali on 24 May 2024
That worked!, thanks very much :). I come from C++ and callbacks work a bit differently there.
Yatharth
Yatharth on 24 May 2024
No worries, it would be great if you accept this answer so that it's reach increases in case of similar queries!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!