Are these the same: set(0, ... and set(groot, ...

23 次查看(过去 30 天)
Let's say I always want graphics to appear a certain way, so I put commands to set graphic preferences in my startup.m file. I have seen some suggestions where these commands are implemented as
set(0, ....
and some where they are implemented as
set(groot, ....
Based on the documentation for groot and this answer, I think they are interchangable, and the graphic preference will be implemented for any figure I make in the current matlab session, but not in future Matlab sessions unless I re-run them in startup. Is this right? I also think "set(0,..." is just an older implementation of groot, which is why I can't find in in the documentation.

采纳的回答

Stephen23
Stephen23 2020-2-20
编辑:Stephen23 2020-2-20
"Are these the same: set(0, ... and set(groot, ..."
No, they are not the same. Not even close to being the same.
  • Zero is a numeric scalar. This means that it is a number.
  • groot is an object, the root of all graphics objects. This means it has methods and properties..
The reason why that outdated Answers thread mentions zero is because it was written just as some huge changes were being made to the graphics engine, which was released with version R2014b:
As you can see, everything changed, not just how the graphics root is referred to.
But, as a matter of convenience and to allow for some backward compatibilty, some of the old ways of referring to old style graphics objects still work, e.g. using number handles. But the documentation, blogs, and answers on this topic make it quite clear that using these outdated syntaxes is discouraged, e.g. "This capability is provided to help make it easier to update code to work with the new graphics system, but should not be used as a permanent fix."
If you want the official TMW explanation of the difference between 0 and groot, read part 2 of this series:
"I have seen some suggestions ..."
Which is a good example of why following random "suggestions" is never as good as reading the documenation.
  2 个评论
Guillaume
Guillaume 2020-2-20
I think that not even close to being the same is a bit strong in the context of the question. set(0,...) and set(groot,...) do achieve exactly the same result, changing the properties of the grahics root. I'm not aware of set(0, ...) stopping to work anytime soon.
However, yes in modern code, you should use groot since numeric graphics handles have long been deprecated and 0 has only been kept for backward compatibilty. On the other hand, some people still prefer to use other backward compatible constructs such as cellfun('size',...) ;)
KAE
KAE 2020-2-20
Thanks. I will replace all instances of "set(0" to "set(groot" in my startup file.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming Utilities 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by