MATLAB: How to link the ‘ZLim’ and ‘CLim’ properties of an axes together in MATLAB 7.9 (R2009b)

graphicshandlehgishandleMATLABpropertiessyncsynchronizetwo

I would like to update the 'CLim' property of my axes, whenever the 'ZLim' property of the same axes changes.

Best Answer

It is possible to use the ADDLISTENER function to execute a function whenever the 'ZLim' property is updated.
a = axes;
surf(peaks);
view(2);
l = addlistener(a, 'ZLim', 'PostSet', @(src, evt) set(a, 'CLim', get(a, 'ZLim')));
Now while the listener 'l' is valid updates to the 'ZLim' property of the axes will cause a corresponding change to the 'CLim' property. The following statement cancels the automatic updates:
delete(l);
Note that the LINKPROP and LINKAXES functions are inapplicable in this situation since those functions are for linking the SAME property of DIFFERENT objects, whereas here we would like to link DIFFERENT properties of the SAME object.