I developed this code to see if the user did zoom in or zoom out on a figure in matlab
function demo3
MainFig = figure;
x = sin(theta) + 0.75*rand(1,300);
y = cos(theta) + 0.75*rand(1,300);
a = 40;
hs=scatter(x,y,a,'MarkerEdgeColor',[0 .5 .5],...
'MarkerFaceColor',[0 .7 .7],...
'LineWidth',1.5);
h = zoom;
set(MainFig, 'WindowScrollWheelFcn', @figure1_WindowScrollWheelFcn);
set(h, 'ActionPostCallback', @mypostcallback);
function mypostcallback(h, eventdata)
disp('INFO: Direction')
h2 = zoom;
get(h2,'Direction')
function figure1_WindowScrollWheelFcn(hObject, eventdata, handles)
if eventdata.VerticalScrollCount > 0
disp ('Scrool Up ')
else
disp ('Scrool Down ')
end
The problem is if I run the code and I use a mouse scroll the information is correct and detect if i do scroll up or down. But if I use the Zoom tools and press in zoom in the information is correct relatively to direction but if I use the mouse scroll up and down the information is the same:
INFO: Direction
ans =
in
I need a code that detects if I do a zoom in or zoom out either with the Zoom tool or with the scrool mouse.
Try this instead:
set(h, 'ActionPostCallback', @mypostcallback);
set(h, 'ActionPreCallback', @myprecallback);
function myprecallback(h, eventdata)
set(h, 'UserData', {eventdata.Axes.XLim, eventdata.Axes.YLim})
function mypostcallback(h, eventdata)
old_lims = get(h, 'UserData');
old_d = cellfun(@diff, old_lims);
new_d = [diff(eventdata.Axes.XLim), diff(eventdata.Axes.YLim)];
disp('INFO: Direction')
if all(old_d == new_d)
disp('No change');
elseif all(old_d-new_d <= 0)
disp('Out')
elseif all(old_d-new_d >= 0)
disp('In')
else
disp('oops, did not expect this!')
end
This saves the axes
limits just before the zoom and compares it with the limits after the zoom. If they're bigger, we zoomed out, if they're smaller, we zoomed in.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.