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.