MATLAB: If I have two plots on the same figure window, how to use the Brush tool to highlight one data point and have the corresponding data point on the other plot highlighted in MATLAB 7.11 (R2010b)

MATLAB

I have two plots side-by-side and they plot data of the same size. I would like to use the Brush tool to highlight one data point on one plot and have the corresponding data point on the second plot highlighted automatically.

Best Answer

You can use the ‘ActionPostCallback’ Brush property. In the callback function, get the index of the data point that is highlighted on the first plot and set the data with the same index on the second plot to be brushed.
See the following example:
function TS_example
% Make two plots side-by-side
fig = figure;
x = 1:10;
y1 = sin(x);
y2 = cos(x);
ax1 = subplot(1,2,1)
plot(y1,'o');
ax2 = subplot(1,2,2)
plot(y2,'o');
h = brush;
% Pass the axes handles to "highlightFcn"
setappdata(gcf,'ax1',ax1);
setappdata(gcf,'ax2',ax2);
% Call the ActionPostCallback function after the selecting data points with
% brush
set(h,'ActionPostCallback',@highlightFcn)
function highlightFcn(es,ed)
ax1=getappdata(gcf,'ax1');
ax2=getappdata(gcf,'ax2');
lh1=get(ax1,'children');
lh2=get(ax2,'children');
bd=get(lh1,'brushdata'); % Get the brushdata from the first axes
set(lh2,'brushdata',bd); % Brush on the same data on the second axes