callbacks, timers and OOP methods: help with using a method as timer callback
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Andrew Newell
el 23 de Abr. de 2011
Could you give an example of a call and describe what is going wrong?
Respuestas (2)
Robert Moss
el 8 de Mayo de 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 comentario
per isakson
el 19 de Mzo. de 2013
Editada: per isakson
el 19 de Mzo. de 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 comentarios
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!