Why does stop(Timer) works, while Timer.stop does not?
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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
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 Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!