Customize Wheel Scroll fxn to work within axes in figure?

Hello, thanks for reading this,
I was wondering, is it possible to create a WindowScrollWheelFcn event exclusive to a axes within a figure I create within a UI? As in, when I cross over the axes in the UI I create and THEN scroll down, I do the callback. Otherwise, I do not activate the callback.
I was thinking I could check the mouse position when I do it in a figure, and if its in the domain of the axes, then do the WindowScrollWheelFcn callback, otherwise do nothing. Would this be the way to go about doing this?
Thanks for your help!

 Accepted Answer

i did a quick test script to show you what is possible.
h=figure
p1=subplot(2,1,1),plot(rand(10))
p2=subplot(2,1,2),plot(rand(4))
set(h,'windowscrollWheelFcn', 'plot(rand(10))');
set(h,'Windowbuttonupfcn', 'gca');
so with the snippit you can see that if you click on subplot 1 and scroll wheel it'll peform the random plot and do the same with subplot 2. since we can determine which subplot (or axes) were clicked the callback function can determine what to perform. I show how to determine which item was clicked using the windowbuttonupfcn. So in the windowscrollwheelfcn function you can set up something that checks what is the active axes and perform the scrollwheel function.

4 Comments

I'll be back once i think of how to not perform the callback when not "hovering" over the figure.
so.... you'll have to do a bit of expiermenting as i don't have much time to play with this much more.... but
i have this script to generate some figure and plots to play with.
h=figure
p1=subplot(2,1,1),plot(rand(10))
p2=subplot(2,1,2),plot(rand(4))
set (gcf, 'WindowButtonMotionFcn', @mouseMove);
set(h,'windowscrollWheelFcn', {@scrollfunc,gca});
Then i have these two function. One is to find where the mouse cursor is, and the second is to only perform some thing if it is within the bounds of X and Y. You'll have to figure out where the XY location of 'Currentpoint' means in relation to the figure you want.
function C = mouseMove (object, eventdata)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
function scrollfunc(object, eventdata,GCA)
get(GCA,'Position');
C=mouseMove (object, eventdata)
insideX = C(1,1)<=4 & C(1,1)>=1;
insideY = C(1,2)>=0 & C(1,2)<=1;
if insideX&insideY
plot(rand(10));
end
I'll try this out, thanks again for your help!
Hello, thanks again for this, I think I'm almost there. I have one last question.
I can use that custom function now, but I was wondering how I can pass the handles sruct (because thats where I have some saved data) to the custom function. Also, I want the custom function to return handles.
So when I activate the custom callback here:
set(h,'windowscrollWheelFcn', {@scrollfunc,gca});
Where do I input the handles struct into the custom function and output handles? I never passed functions this way before, so I don't know how its done here.
Thanks, you've been a big help

Sign in to comment.

More Answers (2)

Thanks! This is perfect.
The lines
set(h,'windowscrollWheelFcn', 'plot(rand(10))');
set(h,'Windowbuttonupfcn', 'gca');
is where the magic happens, right? The first line does the scroll function, and the second one sets the gca to the axes I push on. Is that line of reasoning correct?
Thanks again, this is exactly what I needed!

1 Comment

yes. so the first set for the windowscrollwheefcn will perform what will be after. Before I implemented the mouse positioning i used the windowbuttonupfcn to find which axes was last selected. but i realized you want to utilize the mouse positioning to determine which axes is hovered not clicked.

Sign in to comment.

One last question, if I wanted to customize the callback of windowscrollWheelFcn to something other than plot(rand(10)), can I use the line:
set(h,'windowscrollWheelFcn', @doCustomScrollFunction);
and declare doCustomScrollFunction later to do what I want it to do?

2 Comments

yes exactly, in my second comment where I included the mouse positioning, it calls for the function scrollfunc() where you can do something else. I just use plot(rand) as a temporary do something function.
Thanks again! You were a great help.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 16 Jul 2014

Commented:

on 21 Jul 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!