setting WindowButtonMotionFcn to several subplots. Later command set(gcf, 'WindowButtonMotionFcn'...) work only for last suplot
19 views (last 30 days)
Show older comments
Hi
I have a figure with 2 subplot
f=figure
subplot(2,1,1)
plot(...)
% Then I set WindowButtonMotionFcn with my function highlightPlot_1
set(gcf, 'WindowButtonMotionFcn', @(src,event)highlightPlot_1(hittest(src)));
% if I make a breackpoint here, it works for 1st subplot
% Then I plotting another subplot
subplot(2,1,2)
plot(...)
%and again set WindowButtonMotionFcn with my another function highlightPlot_2
set(gcf, 'WindowButtonMotionFcn', @(src,event)highlightPlot_2(hittest(src)));
% at the end WindowButtonMotionFcn works only for 2nd subplot. It seems like 2nd set(gcf, ...) command overwrites previous one.
% And I understood that I cannot set WindowButtonMotionFcn for specific subplot?
So is it possible to have two working WindowButtonMotionFcn for each of subplot?
...You can say like separate subplots to two figures - prabably it will work but I want to have it in subplots.
Thanks!
Answers (1)
Abhipsa
on 5 Feb 2025 at 5:08
Edited: Abhipsa
on 5 Feb 2025 at 5:12
Hi Yurii, I get that you would like to know if it is feasible to have distinct “WindowButtonMotionFcn” callbacks for each subplot within a single figure.
The “WindowButtonMotionFcn” is a property of the figure, not of individual subplots. This means that setting it up multiple times will overwrite the previous function. Hence, we can use a single “WindowButtonMotionFcn” that determines which subplot the mouse is currently over and calls the appropriate function.
We can create handles to both the plots let’s say “h1” and “h2” and name the subplots as “ax1” and “ax2”.
Now, we need to assign a function to the figure “f” whenever the mouse moves within the figure. This figure will determine the mouse's position relative to the subplots and adjust the appearance of the plots accordingly, such as changing line colors when hovering over different subplots.
The “set” function can be used to achieve this as below:
set(f, 'WindowButtonMotionFcn', @(src, event) handleMouseMotion(src, ax1, ax2, h1, h2));
% You can use gcf instead of f as gcf is “get current figure”
Here, “handleMouseMotion” is a custom function that is designed to perform the specific actions you want to execute on the figure.
You can use the command “>> doc WindowButtonMotionFcn” in MATLAB command to refer to the official documentation.
I hope this helps.
0 Comments
See Also
Categories
Find more on Parametric Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!