Pass newly created object's function to a timer object

11 Ansichten (letzte 30 Tage)
Vincent
Vincent am 30 Jul. 2012
I created a class which reads a file as soon as an object is created. In order to display the progress of reading process, I created a public function (which I prefered to be private by the way). Let's call this function writeProgress,
However, after starting the timer, I receive an warning message:
"feval: Undefined function or method 'obj.writeProgress' for input arguments of type 'timer'".
The timer has been initialized like this:
t = timer('TimerFcn',@obj.writeProgress,'Period',1,'ExecutionMode','fixedDelay');
How can I pass the function "writeProgress"? Write progress needs to be a class member as it has to access private data and I'd rather prefer it to be private itself.

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 30 Jul. 2012
Here is a class that does what you want from the description above. The only difference is that I placed the call to writeProgeress inside of an anonymous function.
What else is different with it from your class?
classdef WP < handle
properties
x
y
end
methods
function obj = WP(varargin)
obj.x = pi;
obj.y = 'hello world';
t = timer('TimerFcn',@(src,evt)obj.writeProgress,'Period',1,'ExecutionMode','fixedDelay');
start(t)
end
end
methods (Access=private)
function writeProgress(obj)
disp(obj.x);
disp(obj.y);
end
end
end
  3 Kommentare
Sean de Wolski
Sean de Wolski am 30 Jul. 2012
Bearbeitet: Sean de Wolski am 30 Jul. 2012
Most callbacks automatically receive two inputs: the source of the callback and some event data. These need to be accounted for. In the above, I accounted for them with the anonymous function and then threw them away because we didn't need them.
Vincent
Vincent am 30 Jul. 2012
well, thanks a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Code Execution finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by