由于 Gunthers 的暗示,我能够自己解决问题。由于 Gunthers 的回答仍然需要进行大量研究,因此我在下面提供了自己的解决方案,以防万一有人在某个时候遇到类似的问题。
首先,我在轴对象中添加了作为 UserData 所需的大部分数据。我的绘图函数(仅用于两个绘图)大致如下所示:
sb1 = subplot(1, 2, 1);
plot(data(:,1), data(:,2));
axis equal;
sb2 = subplot(1, 2, 2);
plot(data(:,4), data(:,3));
set(sb1, 'UserData', struct('projection', 'xy', 'data', data, 'link', [sb2]));
set(sb2, 'UserData', struct('projection', 'th', 'data', data, 'link', [sb1]));
panzoom(sb1, 'setlimits'); % Those two lines ensure that the zoom limits won't get changed
panzoom(sb2, 'setlimits'); % over time.
现在,我将处理程序设置为我的缩放功能:
z = zoom;
set(z, 'ActionPostCallback', @Track.synchronizePlots);
z = pan;
set(z, 'ActionPostCallback', @Track.synchronizePlots);
最后,这就是魔法发生的地方:
function synchronizePlots(obj, ax)
ax = ax.Axes;
ud = get(ax, 'UserData');
if ud.projection == 'xy'
% That is the part discussed in the comments above,
% which is, as I freely admit, not very sensible on a strict
% mathematical point of view. However, the result is good enough for my
% purpose
xrange = get(ax, 'XLim');
yrange = get(ax, 'YLim');
pointsvisible = ud.data(1,:) >= xrange(1) & ...
ud.data(1,:) <= xrange(2) & ...
ud.data(2,:) >= yrange(1) & ...
ud.data(2,:) <= yrange(2);
r = [min(ud.data(4, pointsvisible)), max(ud.data(4, pointsvisible))];
if length(r) == 0 % The trick above may fail if there is no point in the zoom region.
return % in that case we just do nothing.
end
else
r = get(ax, 'XLim'); % Straightforward
end
for a = ud.link % The function does not care about the number of figures that have to be changed.
linkud = get(a, 'UserData');
if linkud.projection == 'xy'
% Again, changing the xy-plot is that only part we have to work.
pointsintime = linkud.data(4,:) >= r(1) & ...
linkud.data(4,:) <= r(2);
xrange = [min(linkud.data(1, pointsintime)), ...
max(linkud.data(1, pointsintime))];
yrange = [min(linkud.data(2, pointsintime)), ...
max(linkud.data(2, pointsintime))];
if length(xrange) > 0
set(a, 'XLim', xrange);
set(a, 'YLim', yrange);
axis(a, 'equal');
end
else
set(a, 'XLim', r);
end
end
希望对某人有所帮助。