the mechanism of different syntax for realizing the same effect

2 次查看(过去 30 天)
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.
  1 个评论
Qiang
Qiang 2024-7-18
Thank you very much for all of the informative and complete answers. I ve updated lots of object hierarchy logics of MATLAB.
Additionally, I've learned that for the set function < set(h,"Properties Name","Properties Value">, h should be an object. That is why < set(ax.XLabel, "String", "Time") > work, but < set(ax.Box, "off") > doesnot work because in the former the XLabel in syntax ax.XLabel is an object while the latter the Box in syntax ax.Box is just a property.
When it come to < ax.XLabel.String = "Time" > and < ax.Box = "off" >, both can work because here the syntax adopts dot notation query. For dot notation, any property can be accessed by using it. The notations after the last dot in these two syntax are both properties. Now, the logics in different situations is the same.
That could be part of the reason that caused my confusion.
Thanks again for the insightfull help.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2024-7-17
I'm not sure your mental model is quite correct, so I want to walk through your question in a bit of detail.
Multiple syntaxes for similar or the same task
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.
That is somewhat true, though IMO we have been getting better at it as time goes on. [I'm one of the people who design reviews some of the new features going into MATLAB, so I am quite biased in this belief. :)]
Where you're more likely to see that nowadays is where we provide a simple way to achieve common tasks quickly but offer more bells and whistles for users who want more control over details. The rng function and the RandStream object are one example of this: if all you want to do is reset the random number generator in MATLAB, just call rng. If you want to do something more complicated, like control exactly how randn constructs its normally distributed numbers from uniformly distributed numbers or create parallel streams that don't interact with one another, you'd need to use RandStream.
box versus set
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 > ??
Not quite. The first of those syntaxes calls the box function. The second calls the set function and does not call the box function at all.
The box function does one thing: it changes whether or not the axes has a box around it.
The set function technically does one thing (it changes the properties of a Handle Graphics object) but that "one thing" is much more flexible and can be more complicated.
While box changes one property of one type of Handle Graphics object, set can change any (writable) property of any Handle Graphics object.
To use a cooking metaphor, box is a potato slicer, something like the Veg-o-Matic. It cuts a potato into sticks suitable for making french fries. It's easy to use (put potato in, press plunger) but it only does one thing. set is a knife. It can cut a potato into french fries, but it also has tons of other uses. But it can take more effort to learn how to use it safely (even professional chefs occasionally cut themselves; if you've watched food competitions on TV you've likely seen it happen) and use it well.
Differences between box and xlabel
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.
xlabel is also a Veg-o-Matic. It has one job to do and it does it. It's easy to use (at its most basic.)
While the Box property of an axes is a potato, and you can easily cut it with the knife of set, the XLabel property is more like a coconut. In order to get at the meat that is the actual string of the label, you need to get through the hull of the coconut that is the Text object that contains that string. If you use the xlabel-o-Matic it "automatically recognizes a coconut" and assumes you just want the coconut meat, so it helpfully skips past the hull to the meat. The set function doesn't assume you're interested just in the coconut meat and so doesn't bypass the hull.
But both box and XLabel are axes properties?
Why is this so as both box and XLabel are the properties of Axis.
Be careful. You're conflating several things here. For boxes:
  • The Box property of an axes controls whether or not MATLAB draws a box around the axes.
  • The box function provides a simple way to change the Box property of the axes.
  • The set function provides a more powerful (but more complicated) interface to change the Box property of the axes.
For the label:
  • The XLabel property of an axes contains a text object representing the label of the X axis.
  • The xlabel function provides a simple way to change the properties of the object stored in the XLabel property of an axes. In its simplest syntax, it changes the String property of the text object stored in the XLabel property of the axes.
  • The set function provides a way to replace the text object stored in the XLabel property entirely if need be. This can be more powerful but can also be more complicated.
Direct access to String property of XLabel
The funny thing is that < ax.XLabel.String = "Time" > is also ok. Can someone help explain? Thanks.
Yes. Let's break it down dot by dot.
ax is the handle of an axes. It has an XLabel property that contains a text object.
ax.XLabel is the text object stored in the XLabel property of the axes. It has a String property that contain the words/characters displayed as the label of the X axis.
ax.XLabel.String is the words/characters displayed as the label of the X axis.
That assignment changes the words/characters displayed as the label of the X axis to "Time". ax.XLabel doesn't change after that statement is executed, just one of its properties does. Using another metaphor, ax.XLabel is a house. Changing who lives in the house (its String property) doesn't change its address (usually[1]).
[1] There are companies that will move houses from one place to another, but that's not the usual pattern for buying a new house.

更多回答(2 个)

Sivsankar
Sivsankar 2024-7-17
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 个评论
Stephen23
Stephen23 2024-7-18
编辑:Stephen23 2024-7-18
"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.

请先登录,再进行评论。


Rahul
Rahul 2024-7-17
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.
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 个评论
Stephen23
Stephen23 2024-7-17
编辑:Stephen23 2024-7-17
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by