Yes, it just depends on how you feel about using global variables. (This is just something I looked at for the first time, so there may be more elegant approaches.) You can capture zoom events and adjust your text widgets based on whether the user is zooming in or out. Suppose you take your block of code (from above) and place it in a new function called moving_ids:
function moving_ids
close all;
global GRID_TEXT_HANDLES;
global X_COORDS;
global Y_COORDS;
X_COORDS=[10000;11000;12000];
Y_COORDS=[20000;21000;22000];
points_id={'p100';'p200';'p300'};
figure(1),scatter(X_COORDS, Y_COORDS, 'b^');grid on;
GRID_TEXT_HANDLES = text(X_COORDS, Y_COORDS+10, points_id);
h = zoom;
set(h,'ActionPreCallback',@prezoom);
set(h,'ActionPostCallback',@postzoom);
In the above, we have set the framework to capture zoom events in the figure. Now you need to define the callbacks. The pre-zoom callback will be used to capture the limits (prior to zoom) of each axis:
function prezoom(obj,event_obj)
global PREZOOM_AXIS_X_LIMITS;
global PREZOOM_AXIS_Y_LIMITS;
PREZOOM_AXIS_X_LIMITS = get(event_obj.Axes,'XLim');
PREZOOM_AXIS_Y_LIMITS = get(event_obj.Axes,'YLim');
We are going to use the above pre-zoom limits to determine how the label position should be adjusted relative to the marker during the post-zoom. Now we handle the post-zoom action which will be used to adjust the text for each marker:
function postzoom(obj,event_obj)
global PREZOOM_AXIS_X_LIMITS;
global PREZOOM_AXIS_Y_LIMITS;
global GRID_TEXT_HANDLES;
global X_COORDS;
global Y_COORDS;
PSTZOOM_AXIS_X_LIMITS = get(event_obj.Axes,'XLim');
PSTZOOM_AXIS_Y_LIMITS = get(event_obj.Axes,'YLim');
prezoomYDiff = abs(PREZOOM_AXIS_Y_LIMITS(2) - PREZOOM_AXIS_Y_LIMITS(1));
pstzoomYDiff = abs(PSTZOOM_AXIS_Y_LIMITS(2) - PSTZOOM_AXIS_Y_LIMITS(1));
for i=1:length(Y_COORDS)
y = Y_COORDS(i);
txtHandle = GRID_TEXT_HANDLES(i);
txtPos = get(txtHandle,'Position');
yDiff = abs(txtPos(2)-y);
newYDiff = min(10,max(1,pstzoomYDiff/prezoomYDiff*yDiff));
set(txtHandle,'Position', [txtPos(1) y+newYDiff txtPos(3)]);
end
If you choose to have a different x-coordinate (for the label) then the above code would have to be adjusted to do something similar for the changing x. Hope that this helps!
Best Answer