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

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 件のコメント

Andrew Newell
Andrew Newell 2011 年 4 月 23 日
Could you give an example of a call and describe what is going wrong?

サインインしてコメントする。

回答 (2 件)

Robert Moss
Robert Moss 2011 年 5 月 8 日

0 投票

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 件のコメント

Robert Moss
Robert Moss 2011 年 5 月 8 日
Sorry, by subfunction I actually meant nested function.

サインインしてコメントする。

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

カテゴリ

タグ

質問済み:

2011 年 4 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by