callbacks, timers and OOP methods: help with using a method as timer callback
1 view (last 30 days)
Show older comments
Hello, world of Matlab wizards.
6 years Matlab user, just started playing with OOP in Matlab. I'm trying to using a method as caklabck for a timer which is located in the constructor, and it doesn't work. What am I missing? Please enlighten me.
Thanks
************************
classdef Vehicle
properties
Speed % [Km/Hour]
Length % [Meter]
Width % [Meter]
Driver % Properties of the driver
Current_Route % Current route
Route_Percent % Which Percent of the route the vehicle is in
Route_Direction % Driving forward or backeard on the routhe
Last_Action_Time % The time stamp for the last action of the vehicle
end
methods
function this = Vehicle(varargin)
this.Speed = varargin{1}; % The speed of the car
this.Current_Route = varargin{2}; % What route the vehicle is on
this.Route_Percent = varargin{3}; % Where on the routeh is the vehicle
this.Route_Direction = varargin{4}; % To what direction is the car moving on the route
this.Last_Action_Time = clock; % Set the time the thisect was creted
Progress_Timer = timer('TimerFcn', @Progress, 'Period', varargin{4}, 'ExecutionMode' ,'FixedRate'); % Sets a timer for progressing vehicle position
start(Progress_Timer) % Starts the timer
end
function this = Progress(this)
if (this.Route_Percent >= 0 && this.Route_Percent <= 100) % If the vehicle does not exceed the route map
this.Route_Percent = this.Route_Percent + ...
this.Route_Direction * this.Speed * etime(clock,this.Last_Action_Time)/3600 / this.Current_Route.Length * 100
this.Last_Action_Time = clock; % Set the time the progress was done
else
this.delete; % Vehicle is no longer relevant
stop(Progress_Timer); % Stops the timer
end
end
end
end
1 Comment
Answers (2)
Robert Moss
on 8 May 2011
Call back functions need to be a subfunction of the method from which they are called. In this case that is the constructor. Move the Progress method inside your constructor and it should be happier.
1 Comment
per isakson
on 19 Mar 2013
Edited: per isakson
on 19 Mar 2013
The creation of the timer object could be
% Sets a timer for progressing vehicle position
Progress_Timer = timer ...
( 'TimerFcn' , @(src,event) Progress(this) ...
, 'Period' , varargin{4} ...
, 'ExecutionMode' , 'FixedRate' );
Possibly, Progress(this,src). The signature of Progress must match.
...
this.delete; % Vehicle is no longer relevant
stop(Progress_Timer); % Stops the timer
No delete-method is defined for Vehicle. (Inherit from handle.)
Progress_Timer not defined here.
0 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!