MATLAB: How to link an image and a histogram of that image so the histogram only represents what is shown in the image, i.e. if I zoom in on the image, the histogram updates

image histogram linkdata zoomImage Processing Toolbox

I have a gui with two axes, an image (created using imagesc) and a histogram of that image. I want to add a menu item that will allow the user to turn on/off the ability to link the histogram to the image so if the user zooms in/out, the histogram automatically updates with the image data that is shown (and ignore the image data that is not shown).
I am using Matlab 2016a.
Any suggestion is welcome.
Thanks!

Best Answer

I was able to get this to work by setting the ActionPostCallback for the zoom function to call a custom function.
set(zoom(obj.ax1), 'ActionPostCallback',@(x,y) obj.redrawHistogram(obj.ax1));
and inside the redrawHistogram function I redraw the histogram based on the data being displayed.
Related Question