world of Matlab wizards.
6 years Matlab user, just started playing with OOP in Matlab. I'm trying to using a method as callback for a timer which is located in the constructor, and it doesn't work. What am I missing? Please enlighten me.
Thanks
classdef Vehicle
% Vehicle
% Vehicle superclass
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
开发者_StackOverflow社区 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
Methods aren't like subfunctions. That means you want to pass the object to the timer function.
Progress_Timer = timer('TimerFcn',@()Progress(this))
Also, you want vehicle
to be a handle class, so that it this
in the function handle gets updated whenever the object gets updated.
However, instead of using a timer, you may instead want to use set-methods that update the status of the vehicle whenever one of the properties change. This will be a lot more transparent.
精彩评论