필터 지우기
필터 지우기

Why does stop(Timer) works, while Timer.stop does not?

조회 수: 2 (최근 30일)
Stefan
Stefan 2018년 5월 16일
편집: Guillaume 2018년 5월 16일
Using Timer Objects, I noticed some behaviour I could not explain.
Creating a timer with fixed rate, I was not able to stop it with Timer.stop(). However, stop(Timer) does work. What am I missing?
Timer =timer('Period', 1);
Timer.TimerFcn = @(myTimerObj, thisEvent)disp('1 second has elapsed');
Timer.ExecutionMode = 'fixedRate';
Timer.start();
pause(2)
stop(Timer); %<----works
%Timer.stop; %<----does not work

채택된 답변

Guillaume
Guillaume 2018년 5월 16일
편집: Guillaume 2018년 5월 16일
That's certainly an interesting behaviour and it took me a while to figure out what is actually happening. It's an unintended side effect of the matlab.mixin.SetGet abstract class from which timer derives. In my opinion, it's a bug, which I'll report. Whether mathworks sees it this way and certainly whether they do anything about it is anybody's guess.
The timer class derives from matlab.mixin.SetGet which gives it an interface that allows you to read/write properties of the class using e.g. get(Timer, 'Period') and set(Timer, 'Period', value). By default, this class also also you to abbreviate the property name to anything that is non-ambiguous. It also ignore case. So, in the above example, you can also write get(Timer, 'p') since the Period property is the only one that starts with p, regardless of case. The same happens when you use dotted notation to access properties, i.e. you can write Timer.p = 5. edit: That last sentence was wrong, matlab.mixin.SetGet does not affect dotted notation, it's the undocumented class attribute TruncatedProperties that does that.
The scoping rules for the syntax method(obj) and obj.method are slightly different. In the first case, matlab only search for functions and in the second case it also looks at properties (note that this is not documented, I may have things slightly wrong here). So when you write Timer.stop, matlab looks at the properties that starts with stop. It finds one and only one and thus ends up invoking get(Timer, 'StopFcn') and returns you the content of that property instead of invoking the stop method.
This doesn't happens with Timer.start because there are two properties that starts with start. Since that doesn't resolve to a unique property matlab ends up invoking the start method.
edit: After some more testing, I was slightly wrong about the root of the problem. It's not matlab.mixin.SetGet that is the culprit. It's actually one of the class attribute, TruncatedProperties=true that is the cause of the problem. TruncatedProperties is undocumented unfortunately.
  댓글 수: 2
Jan
Jan 2018년 5월 16일
Preferring the property 'StopFcn' to the method stop is a bad idea. I consider this as a bug. Trying to make Matlab smart by guessing, what the user wants by a magic auto-completion, is not a reliable design idea, but a source of bugs.
+1, thanks for this investigation, Guillaume.
Guillaume
Guillaume 2018년 5월 16일
Yes, I consider it a bug, and have reported it as such. While it can make sense to allow shortened properties for set and get since you don't get autocompletion, I do think it shouldn't be allowed for dotted notation (which is what TruncatedProperties allow) since tab completion gives you the full name.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by