callbacks, timers and OOP methods: help with using a method as timer callback

1 次查看(过去 30 天)
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

回答(2 个)

Robert Moss
Robert Moss 2011-5-8
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.

per isakson
per isakson 2013-3-19
编辑:per isakson 2013-3-19
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.
It is a pain to debug code like this. See previous Debug code invoked by timer

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by