# 带有多个图的MATLAB GUI Zoom

``````y: height = [10,9,4,6,3];
x: time = [1,2,3,4,5];
``````

``````y: latitude = [10,20,30,40,50];
x: longitude = [11,12,13,14,15];
``````

``````function syncLimits(masterAxes,slaveAxes)
% Sync a slave axes that is plot related data.
% Assumes each data point in slave corresponds with the data point in the
% master at the same index.

% Find limits of controlling plot
xRange = xlim(masterAxes);
% Get x data
x1Data = get(get(masterAxes,'children'),'XData');
% Find data indices corresponding to these limits
indices = x1Data >= xRange(1) & x1Data <= xRange(2);
if any(indices)
% Set the limits on the slave plot to show the same data range (based
% on the xData index)
x2Data = get(get(slaveAxes,'children'),'XData');
y2Data = get(get(slaveAxes,'children'),'YData');
minX = min(x2Data(indices));
maxX = max(x2Data(indices));
minY = min(y2Data(indices));
maxY = max(y2Data(indices));
% Set limits +- eps() so that if a single point is selected
% x/ylim min/max values aren't identical
xlim(slaveAxes,[ minX - eps(minX) maxX + eps(maxX)  ]);
ylim(slaveAxes,[ minY - eps(minY) maxY + eps(maxY)  ]);
end

end
``````

``````height = [10,9,4,6,3];
time = [1,2,3,4,5];

latitude = [10,20,30,40,50];
longitude = [11,12,13,14,15];

% Plot Height v Time
h1 = figure;
a1 = gca;
plot(time,height);
title('Height v Time');

% Plot Lat v Long
figure;
a2 = gca;
plot(longitude, latitude);
title('Lat v Long')

% Set-up Callback to sync limits
zH = zoom(h1);
pH = pan(h1);
set(zH,'ActionPostCallback',@(figHandle,axesHandle) syncLimits(axesHandle.Axes,a2));
set(pH,'ActionPostCallback',@(figHandle,axesHandle) syncLimits(axesHandle.Axes,a2));
``````

0条评论