简体   繁体   English

当我使用 matlab 的缩放工具时,我如何检测是否使用鼠标滚动?

[英]When I use the Zoom tools of matlab how i detect if i use mouse scroll?

I developed this code to see if the user did zoom in or zoom out on a figure in matlab我开发了这段代码来查看用户是否在 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.这将保存缩放前的axes限制,并将其与缩放后的限制进行比较。 If they're bigger, we zoomed out, if they're smaller, we zoomed in.如果它们更大,我们就缩小,如果它们更小,我们就放大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM