|
"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
|