Say I have four plots h1-h4 and want to link all their x-axes. For that I can use linkaxes([h1 h2 h3 h4], 'x')
. However, in addition I want to link h1 and h3's y-axes (and also h2's to h4's). Now when I use linkaxes([h1 h3], 'y')
the x-axes linking is lost. How can both linkings be achieved at the same time?

- 25,759
- 22
- 127
- 221
2 Answers
Lines 73 and 74 of linkaxes
are:
%# Remove any prior links to input handles
localRemoveLink(ax)
I suggest you create a new function, myLinkaxes
, via "Save As...", where you comment out line 74. Even better, myLinkaxes
could accept an additional input argument "keep", which is used in an if-clause around lines 73 and 74, i.e. if keep
is 1, localRemoveLink
is not called.
This should work if you separately link x and y-axes, but if you use the 'xy'
argument before or after, there might be trouble.

- 74,690
- 10
- 137
- 177
-
@Tobias Kienzler: Thanks for the precision. I checked in R2011a pre-release. – Jonas Mar 30 '11 at 11:56
In addition to the solution provided by Jonas, I think that it is also worth mentioning the lower-level function linkprop
, which is capable of linking seemingly arbitrary properties of graphics objects.
For this particular question, the desired effect can be achieved via the following sequence of commands:
linkaxes([h1 h2 h3 h4], 'x');
lnkObj = linkprop([h1 h3], 'YLim');
For demonstrative purposes (and because linkprop
is new to me), this example is extremely simple. Please see the documentation for more details and a more complex example.
It may also be worth mentioning here that linkprop
returns a link object, which (according to the previous link) "must exist within the context where you want property linking to occur"; in particular, it seems to be the case that linking will cease if all references to the link object disappear, thus the reason for assigning the link object to a variable above. Moreover, a reference to the created link object is necessary for changing the details of how the corresponding graphics objects are linked (i.e., which objects' properties are linked by the given link object); see Updating a Link Object for more information (including a list of functions designed specifically for the purpose of performing such updates).

- 438
- 4
- 9