您需要一个将高度和时间映射到纬度和经度的函数,然后根据映射值设置限制。
以下函数完成这项工作:
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
然后,您可以获取高度 v 时间图,以便在缩放或平移时调用此函数。
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));
如果您的绘图是由函数生成的,代码会更简单,因为您可以嵌套 syncLimits 函数并直接使用时间、纬度和经度数据。您还可以将此数据传递给 syncLimits 函数,这将再次减少代码,但您只需编写一次syncLimits(我已经完成了!)。