Let timer access another object's function

조회 수: 6 (최근 30일)
Vincent
Vincent 2012년 7월 30일
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.

답변 (1개)

per isakson
per isakson 2012년 7월 31일
This works with R2012a
classdef test_timer < handle
properties
t
end
methods
function this = test_timer()
this.t = timer( 'TimerFcn',@(s,e) writeProgress(this,s,e) ...
, 'Period' , 1 ...
, 'ExecutionMode' , 'fixedDelay' );
start( this.t )
end
end
methods ( Access = private )
function writeProgress( this, varargin ) %#ok<MANU>
disp('writeProgress')
end
end
end
I cannot say why I use this syntax in my code.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by