the mechanism of different syntax for realizing the same effect
Show older comments
hi, i wanted to ask is there some logics for remembering the syntax in Matlab, because sometimes i found the syntax have different logics, which confused myself. For example, < box on/off > is a simplified syntax in controlling whether the axis box is on. There are two syntax with different logics on controlling box. One is < box(ax,"off") >, and the other one is < set(ax,"box","off") > . PS. The former is probably the full form of < box off > ?? These two forms have different starting point within it. But when it comes to setting xlabel, it seems we could only use < xlabel("Time") >, and < set(ax,"XLabel","Time") > doesnot work. Why is this so as both box and XLabel are the properties of Axis. The funny thing is that < ax.XLabel.String = "Time" > is also ok. Can someone help explain? Thanks.
Accepted Answer
More Answers (2)
Sivsankar
on 17 Jul 2024
Hi
I could try to provide some logic for understanding the syntax of these commands.
- Simplified syntax like ‘box on’ or ‘xlabel('Time')’ is designed for convenience and quick usage.
- Full syntax like ‘box(ax, 'on')’ and ‘xlabel(ax, 'Time')’ allows you to specify the axes object explicitly, which is useful when you have multiple axes.
The set function is a more general way to set properties of graphics objects. It can be used for a wide range of properties, not just those with specific commands.
For box, both box(ax, 'off') and set(ax, 'Box', 'off') are valid because 'Box' is a property of the axes object and 'Box' takes string values.
Now comes the interesting part. The XLabel property of an axes object is not a simple string property but an object itself (a text object). Since XLabel is not a string you can not set it to ‘Time’ (in this case). However, you can use set for XLabel like this
set(get(ax, 'XLabel'), 'String', 'Time')
You can go inside the 'String' property of the 'Xlabel' and change it
Thanks
3 Comments
Qiang
on 17 Jul 2024
"Box has only one property, namely on/off state, and accepts string as value"
BOX is a property of an AXES object. BOX is not an object, it has no properties of its own.
In contrast XLABEL is a text object, which does have properties.
"Then, by the way, what other properties does XLabel have?"
Here are all axes object properties:
The XLABEL description states that it is a text object and has a link the text object properties:
That page lists many text properties that you can use:
xlabel('hello')
ylabel('world')
axh = gca();
axh.XLabel.Color = [1,0,0];
Summary: the documentation states what properties graphics objects have.
Qiang
on 18 Jul 2024
Rahul
on 17 Jul 2024
Hi @Qiang
I was able to replicate your doubts regarding MATLAB syntax. Here is the explanation for your query:
When you add an axes like,
ax = axes;
% this creates an axes object named 'ax'
Now if you try to set the 'box' property of this axes by either:
box(ax,"off");
% or
ax.Box = "off";
% or
set(ax, "box", "off");
All three of these syntax work for setting the 'box' property of the axes to 'off' as the 'box' property belongs to the 'OnOffSwitchState' class of MATLAB which only requires string input to set it's state.
You can refer to this for more information regarding the 'OnOffSwitchState' class: https://www.mathworks.com/help/releases/R2024a/matlab/ref/matlab.lang.onoffswitchstate-class.html?searchHighlight=OnOffSwitchState&s_tid=doc_srchtitle
On the other hand, the 'xlabel' propery of the axes 'ax' is an object of 'text' class which requires multiple arguments to be set directly.
You can refer to this for more information regarding the 'text' class:
Since in your query you only need to change the 'string' property of the 'xlabel'. Hence you cannot directly use
set(ax,"XLabel","Time");
Here you haven't specified which property of the 'xlabel' is to be set to 'Time'.
Instead you can choose the following commands to achieve your desired result:
set(ax.XLabel,"String", "Text") % Here ax.Xlabel is specified in the first argument and 'string' propery is specified in the second argument.
% or
ax.XLabel.String = "Time" % This also works as the 'string' property of the 'xlabel' is specified to be changed.
Hope this helps!
3 Comments
Qiang
on 17 Jul 2024
"Since both box and XLabel are the properties of axis, why they have different invoke manner?"
The fact that they are properties of something is not the point. What is much more relevant is what kind of thing are they. That is what you need to start thinking about.
You already have the explanation given to you three times. Here it is again:
- BOX is a property which can have value 'off' or a logical scalar.
- XLABEL is a property which itself is a graphics object. Graphics objects have lots of properties of their own!!! XLABEL is a text object. Of course text objects have a STRING property which you can SET, there is absolutely nothing stopping you from doing so!
Actually the manner of invoking them is exactly the same: of course you have to refer to the specific graphics object (which might be nested within other obejcts), but this is entirely consistent and expected behavior. Graphics objects are not all nested only one layer deep from an axes object (which seems to be the root of your misunderstanding).
Remember that the axes object is inside a figure object which itself is inside the graphics root object. Read more about graphics objects and how they are structured/nested inside each other:
Do you expect to be able to SET the axes BOX property by calling SET(GROOT,'BOX',true) ? Of course not, that would make no sense: the graphics root has no BOX property! To SET the property of an axes object you need to call SET with the axes object. In exactly the same way, if you want to set the STRING property of a text object, then you need to call SET with that text object (not call SET with some axes object, which itself might contain multiple text objects). So the manner of calling them is exactly the same.
Qiang
on 17 Jul 2024
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!