MATLAB图形用户界面多图缩放
我在MatlabGUI中有两个绘图,我想将它们链接在一起,这样一个绘图上的缩放会缩放另一个绘图 与我在链接图上看到的类似问题不同,我的x数据或y数据都不是由两个图共享的,而是相关的MATLAB图形用户界面多图缩放,matlab,plot,Matlab,Plot,我在MatlabGUI中有两个绘图,我想将它们链接在一起,这样一个绘图上的缩放会缩放另一个绘图 与我在链接图上看到的类似问题不同,我的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]
我的数据包括一架飞机在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];
如果用户在绘图1
上放大x轴,例如显示飞行的前3秒,我希望缩放绘图2
的x轴和y轴,以便仅显示经度和纬度阵列中的前3个经度和纬度坐标
这可能吗 在您发布的示例中,x轴实际上是相同的,即使它们不是相同的变量名,因此您仍然可以使用
linkaxes
链接它们,它只链接限制,而不链接用于打印的变量
h_height = figure(1);
plot(h_height,time,height)
h_location = figure(2);
plot(h_location,longitude,latitude)
linkaxes([h_height h_location],'x')
其中一个的
xlim
中的任何更改都会改变另一个。在您发布的案例中,x轴实际上是相同的,即使它们不是相同的变量名,因此您仍然可以使用linkaxes
链接它们,它只链接限制,而不是用于打印的变量
h_height = figure(1);
plot(h_height,time,height)
h_location = figure(2);
plot(h_location,longitude,latitude)
linkaxes([h_height h_location],'x')
其中一个的
xlim中的任何更改都会更改另一个。您需要一个函数来将高度和时间映射到纬度和经度,然后根据映射的值设置限制
以下函数执行此任务:
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
如果绘图是由函数生成的,则代码会更简单,因为可以嵌套syncLimits函数并直接使用时间、纬度和经度数据。您还可以将此数据传递到syncLimits函数中,该函数的代码也会少一点,但您只需编写一次syncLimits(我已经这样做了!)。您需要一个函数,该函数将高度和时间映射到纬度和经度,然后根据映射的值设置限制
以下函数执行此任务:
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
如果绘图是由函数生成的,则代码会更简单,因为可以嵌套syncLimits函数并直接使用时间、纬度和经度数据。您还可以将此数据传递到syncLimits函数中,该函数的代码也会少一点,但您只需编写一次syncLimits(我已经这样做了!)。我的意思不是让经度数组与时间数组相同。你的答案现在还能用吗?我改变了它们。如果有什么方法我可以从一个图中得到限制,我可以用某种onClick函数更新另一个图。在我看来,你可以用两种方法中的一种来处理。首先,您可以按照您的建议,在每个绘图中插入一个回调函数,以获得其中一个的限制,然后适当地缩放另一个。第二,如果限制总是彼此缩放或移动,则可以定义缩放和/或移动参数,并将其应用于x
变量之一,然后使用get
和set
更改轴标签。有了这个选项,我不知道链接轴是否也会链接属性,我的MATLAB副本正在工作,所以我无法测试它。我不是想让经度数组与时间数组相同。你的答案现在还能用吗?我改变了它们。如果有什么方法我可以从一个图中得到限制,我可以用某种onClick函数更新另一个图。在我看来,你可以用两种方法中的一种来处理。首先,您可以按照您的建议,在每个绘图中插入一个回调函数,以获得其中一个的限制,然后适当地缩放另一个。第二,如果限制总是彼此缩放或移动,则可以定义缩放和/或移动参数,并将其应用于x
变量之一,然后使用get
和set
更改轴标签。有了这个选项,我不知道链接轴是否也会链接属性,我的MATLAB副本正在工作,所以我无法测试它。