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

采纳的回答

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 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by