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

17 views (last 30 days)
KAE
KAE on 20 Feb 2020
Commented: KAE on 20 Feb 2020
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.

Accepted Answer

Stephen23
Stephen23 on 20 Feb 2020
Edited: Stephen23 on 20 Feb 2020
"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 Comments
Guillaume
Guillaume on 20 Feb 2020
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 on 20 Feb 2020
Thanks. I will replace all instances of "set(0" to "set(groot" in my startup file.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming Utilities in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!