我在MATLAB gui中有2个图,我想将它们链接在一起,所以一个图的缩放会放大另一个图。
与我在链接图上看到的类似问题不同,我的x数据或y数据都不由任何一个图共享,但它是相关的。
我的数据包括飞机在5秒钟内飞过地面的高度。
地块1:土地高度
y: height = [10,9,4,6,3];
x: time = [1,2,3,4,5];
地块2:土地坐标
y: latitude = [10,20,30,40,50];
x: longitude = [11,12,13,14,15];
如果用户在上放大x轴Plot 1
(例如显示飞行的前3秒),则我想缩放x和y轴,Plot 2
因此仅显示经度和纬度数组中的前3个经度和纬度坐标。
这可能吗?
您需要一个函数,将高度和时间映射到纬度和经度,然后根据映射值设置极限。
以下功能可以完成此任务:
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(并且我已经完成了!)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句