Thread Subject: Clickable subplots

Subject: Clickable subplots

From: G.A.M.

Date: 19 Aug, 2007 22:36:32

Message: 1 of 7

Hi, I have a figure with many small subplots (as many as 80
or more). I would like to add functionality that would allow
me to double click a subplot to display a full size plot
with more details. Any suggestions on how to do this in an
m-file?

Subject: Clickable subplots

From: Titus

Date: 20 Aug, 2007 15:30:39

Message: 2 of 7


"G.A.M. " <gam32225@yahoo.co> schrieb im Newsbeitrag
news:faaglg$4k6$1@fred.mathworks.com...
> Hi, I have a figure with many small subplots (as many as 80
> or more). I would like to add functionality that would allow
> me to double click a subplot to display a full size plot
> with more details. Any suggestions on how to do this in an
> m-file?

Hi,
use the ButtonDownFunction-property of the subplot:
h = subplot(n,m,i);
set(h, 'ButtonDownFcn', @mycallbackfcn)

and in mycallbackfcn.m:

function mycallbackfcn(hObject, eventdata)
hFig = get(hObject, 'parent');
if strcmp(get(hFig,'SelectionType'),'open')
  % this was a double click, do something
  disp('do something')
end

Hope, this helps,
Titus


Subject: Clickable subplots

From: G.A.M.

Date: 20 Aug, 2007 16:47:57

Message: 3 of 7

Hi,
Thanks for your very helpful reply. I appreciate it very
much! I'm running into a few issues implementing it, however.

The first issue was that setting the callback fcn, as in:
set(h, 'ButtonDownFcn', @mycallbackfcn)

is somehow undone by calling plot(...) in the next line of
code. I verified by stepping through the code. Before
calling set, ButtonDownFcn is empty as shown here:

K>> get(hOS, 'ButtonDownFcn')
ans = ''

after calling set, it is set as shown here:

K>> get(hOS, 'ButtonDownFcn')

ans = @GenerateFullSizePlotFromSubplot

then after calling plot, the ButtonDownFcn is unset
automatically:

K>> get(hOS, 'ButtonDownFcn')
ans = ''

I fixed this by reversing the order of the lines - I plot to
the axes first, then set the ButtonDownFcn. However, the
fact that this happens concerns me. It tells me that I don't
really understand something.

Now I am hitting a few more issues that I have not solved.

When I call
mydata = get(gca,'UserData');
I get []
However, using the property inspector, I can see that the
UserData exists and is correct. It is a string. And gca
returns a value of 534.0007, so I assume gca is returning a
valid subplot.

Any idea how I can get the UserData (or any other property)
from the callback function? What should I look for in debugging?

The other strange thing is that some of my subplots respond
to mouse clicks and some do not. I set ButtonDownFcn on all
of them the same - I do it in a loop. Some subplots seem to
lose the callback and some don't. That's really strange. Any
ideas what I should look for?

Finally, the following code isn't working for me:
if strcmp(get(hFig,'SelectionType'),'open')

Even on a double click the comparison fails.

For testing I am simply responding to a single click, but I
would like to know what the problem is.
Thanks.

"Titus" <titus.edelhofer@mathworks.de> wrote in message
<facc2p$8ve$1@fred.mathworks.com>...
>
> "G.A.M. " <gam32225@yahoo.co> schrieb im Newsbeitrag
> news:faaglg$4k6$1@fred.mathworks.com...
> > Hi, I have a figure with many small subplots (as many as 80
> > or more). I would like to add functionality that would allow
> > me to double click a subplot to display a full size plot
> > with more details. Any suggestions on how to do this in an
> > m-file?
>
> Hi,
> use the ButtonDownFunction-property of the subplot:
> h = subplot(n,m,i);
> set(h, 'ButtonDownFcn', @mycallbackfcn)
>
> and in mycallbackfcn.m:
>
> function mycallbackfcn(hObject, eventdata)
> hFig = get(hObject, 'parent');
> if strcmp(get(hFig,'SelectionType'),'open')
> % this was a double click, do something
> disp('do something')
> end
>
> Hope, this helps,
> Titus
>
>

Subject: Clickable subplots

From: Steven Lord

Date: 20 Aug, 2007 17:24:19

Message: 4 of 7


"G.A.M. " <x0zero@gmail.com> wrote in message
news:facgjt$6kf$1@fred.mathworks.com...
> Hi,
> Thanks for your very helpful reply. I appreciate it very
> much! I'm running into a few issues implementing it, however.
>
> The first issue was that setting the callback fcn, as in:
> set(h, 'ButtonDownFcn', @mycallbackfcn)
>
> is somehow undone by calling plot(...) in the next line of
> code. I verified by stepping through the code. Before
> calling set, ButtonDownFcn is empty as shown here:
>
> K>> get(hOS, 'ButtonDownFcn')
> ans = ''
>
> after calling set, it is set as shown here:
>
> K>> get(hOS, 'ButtonDownFcn')
>
> ans = @GenerateFullSizePlotFromSubplot
>
> then after calling plot, the ButtonDownFcn is unset
> automatically:
>
> K>> get(hOS, 'ButtonDownFcn')
> ans = ''
>
> I fixed this by reversing the order of the lines - I plot to
> the axes first, then set the ButtonDownFcn. However, the
> fact that this happens concerns me. It tells me that I don't
> really understand something.

http://www.mathworks.com/support/solutions/data/1-1B5UM.html?solution=1-1B5UM

There is one additional workaround you can use instead of the two in that
technical solution: use the lower-level line plotting function LINE instead
of the higher-level plotting function PLOT. PLOT calls NEWPLOT; LINE does
not.


>> close all
>> set(gca, 'Tag', 'Hello');
>> plot(1:10);
>> get(gca, 'Tag')
ans =
     ''

>> close all
>> set(gca, 'Tag', 'Hello');
>> line(1:10, 1:10);
>> get(gca, 'Tag')
ans =
Hello


> Now I am hitting a few more issues that I have not solved.
>
> When I call
> mydata = get(gca,'UserData');
> I get []
> However, using the property inspector, I can see that the
> UserData exists and is correct. It is a string. And gca
> returns a value of 534.0007, so I assume gca is returning a
> valid subplot.
>
> Any idea how I can get the UserData (or any other property)
> from the callback function? What should I look for in debugging?

Don't use GCA in a callback like this. GCA could change between when the
callback triggers and when it calls GCA. [I don't know if that's what's
happening here, but it wouldn't surprise me.]


close all
h1 = subplot(2, 1, 1);
set(h1, 'Tag', 'subplot1', 'ButtonDownFcn', 'pause(2); get(gca, ''Tag'')')
h2 = subplot(2, 1, 2);
set(h2, 'Tag', 'subplot2')
% Now click on the first subplot, then immediately click on the second
subplot and note which axes's Tag was displayed.


Instead, use either the handle that MATLAB passes into your ButtonDownFcn or
(if you're using something like the above) use GCBO, which returns the
object whose callback function is executing.


close all
h1 = subplot(2, 1, 1);
set(h1, 'Tag', 'subplot1', 'ButtonDownFcn', 'pause(2); get(gcbo, ''Tag'')')
h2 = subplot(2, 1, 2);
set(h2, 'Tag', 'subplot2')
% Now click on the first subplot, then immediately click on the second
subplot.

% or

close all
h1 = subplot(2, 1, 1);
set(h1, 'Tag', 'subplot_axes1', 'ButtonDownFcn', @(h, eventdata) disp(get(h,
'Tag')))
h2 = subplot(2, 1, 2);
set(h2, 'Tag', 'subplot_axes2', 'ButtonDownFcn', @(h, eventdata) disp(get(h,
'Tag')))


> The other strange thing is that some of my subplots respond
> to mouse clicks and some do not. I set ButtonDownFcn on all
> of them the same - I do it in a loop. Some subplots seem to
> lose the callback and some don't. That's really strange. Any
> ideas what I should look for?

Do you plot into some of your subplots and not others?

> Finally, the following code isn't working for me:
> if strcmp(get(hFig,'SelectionType'),'open')
>
> Even on a double click the comparison fails.


close all
set(gca, 'ButtonDownFcn', @(h, eventdata) disp(get(gcbf, 'SelectionType')))
% Now double-click on the axes and see how many lines of text are displayed


> For testing I am simply responding to a single click, but I
> would like to know what the problem is.

Take a look at the Interruptible and BusyAction properties of the axes. You
may want to have your callback function pause for a short time before it
check the figure's SelectionType property, so that it only 'counts' the
double-click not the single-click and then the double-click.

--
Steve Lord
slord@mathworks.com


Subject: Clickable subplots

From: G.A.M.

Date: 20 Aug, 2007 17:51:23

Message: 5 of 7

Hi Steve,
Thanks for the very useful information. My responses are below.

"Steven Lord" <slord@mathworks.com> wrote in message ''
>
http://www.mathworks.com/support/solutions/data/1-1B5UM.html?solution=1-1B5UM
>

Now I do understand that issue. That's a good link. Thanks.

>
> > The other strange thing is that some of my subplots respond
> > to mouse clicks and some do not. I set ButtonDownFcn on all
> > of them the same - I do it in a loop. Some subplots seem to
> > lose the callback and some don't. That's really strange. Any
> > ideas what I should look for?
>
> Do you plot into some of your subplots and not others?

I plot into all of them the same. However, it looks as if
the issue is related to exactly where I click with the
mouse. If I happen to click on a line series (i.e., the plot
object itself), the callback fcn is not called. However, if
I click on other areas (the background, the axes, etc.), the
callback is called. So it looks to me like the callback
associated with the subplot (the axes) does not get called
if the mouse click happens to land on the plot itself.

What is the solution? Attach the same callback to the plot
and the subplot? I'm guessing there is a more elegant way...

Thanks for your tips.


Subject: Clickable subplots

From: Steven Lord

Date: 20 Aug, 2007 18:05:11

Message: 6 of 7


"G.A.M. " <x0zero@gmail.com> wrote in message
news:fackar$51d$1@fred.mathworks.com...

*snip*

> I plot into all of them the same. However, it looks as if
> the issue is related to exactly where I click with the
> mouse. If I happen to click on a line series (i.e., the plot
> object itself), the callback fcn is not called. However, if
> I click on other areas (the background, the axes, etc.), the
> callback is called. So it looks to me like the callback
> associated with the subplot (the axes) does not get called
> if the mouse click happens to land on the plot itself.
>
> What is the solution? Attach the same callback to the plot
> and the subplot? I'm guessing there is a more elegant way...

The easy way to get this to work is to set the HitTest property for your
plot to 'off'.

http://www.mathworks.com/support/solutions/data/1-1B03X.html?solution=1-1B03X

--
Steve Lord
slord@mathworks.com


Subject: Clickable subplots

From: G.A.M.

Date: 21 Aug, 2007 00:20:26

Message: 7 of 7

Using the tips suggested in this thread I got everything
working. I now have clickable subplots. Thank you.


"Steven Lord" <slord@mathworks.com> wrote in message
<facl4n$h1r$1@fred.mathworks.com>...
>
> "G.A.M. " <x0zero@gmail.com> wrote in message
> news:fackar$51d$1@fred.mathworks.com...
>
> *snip*
>
> > I plot into all of them the same. However, it looks as if
> > the issue is related to exactly where I click with the
> > mouse. If I happen to click on a line series (i.e., the plot
> > object itself), the callback fcn is not called. However, if
> > I click on other areas (the background, the axes, etc.), the
> > callback is called. So it looks to me like the callback
> > associated with the subplot (the axes) does not get called
> > if the mouse click happens to land on the plot itself.
> >
> > What is the solution? Attach the same callback to the plot
> > and the subplot? I'm guessing there is a more elegant way...
>
> The easy way to get this to work is to set the HitTest
property for your
> plot to 'off'.
>
>
http://www.mathworks.com/support/solutions/data/1-1B03X.html?solution=1-1B03X
>
> --
> Steve Lord
> slord@mathworks.com
>
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
buttondownfcn G.A.M. 20 Aug, 2007 13:55:08
uicontrols G.A.M. 20 Aug, 2007 13:55:08
figures G.A.M. 19 Aug, 2007 18:40:19
subplots G.A.M. 19 Aug, 2007 18:40:19
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com