Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting clicks on axes with ButtonDownFcn

Subject: Counting clicks on axes with ButtonDownFcn

From: Camille Couzi

Date: 24 Aug, 2010 17:28:23

Message: 1 of 3

Hi Matlabers!

I am wondering how I could count how many times I have cliked on an axes, within my ButtonDownFcn. For the moment, I have that:
(I have figure 'h.fig' that contains two subplots 'h.cp1' and 'h.cp2'). this is my buttondownfcn:

function bdfcn(hObject,eventdata,h)
counter=0; %initialization of the counter

seltype = get(h.fig,'selectiontype');
if ( h.cp1==gcbo || h.cp2==gcbo )
    switch seltype
        case 'normal'
            counter=counter+1;
end
end

The thing is that my initialization is not well placed, I don't know where I have to put it... like it is now, everytime I call my bdfcn.m function, counter restarts at 0, so I have always counter=1...

If someone could help me, that would be great.

Thanks!
Camille.

Subject: Counting clicks on axes with ButtonDownFcn

From: someone

Date: 24 Aug, 2010 17:40:25

Message: 2 of 3

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i50vfn$btd$1@fred.mathworks.com>...
> Hi Matlabers!
>
> I am wondering how I could count how many times I have cliked on an axes, within my ButtonDownFcn. For the moment, I have that:
> (I have figure 'h.fig' that contains two subplots 'h.cp1' and 'h.cp2'). this is my buttondownfcn:
>
> function bdfcn(hObject,eventdata,h)
> counter=0; %initialization of the counter
>
> seltype = get(h.fig,'selectiontype');
> if ( h.cp1==gcbo || h.cp2==gcbo )
> switch seltype
> case 'normal'
> counter=counter+1;
> end
> end
>
> The thing is that my initialization is not well placed, I don't know where I have to put it... like it is now, everytime I call my bdfcn.m function, counter restarts at 0, so I have always counter=1...
>
> If someone could help me, that would be great.
>
> Thanks!
> Camille.

% Make counter a persistent variable.

doc persistent

% to see how it is done.
% Modify your code something like:

function bdfcn(hObject,eventdata,h)

persistent counter
if isempty counter
   counter=0; %initialization of the counter
end

Subject: Counting clicks on axes with ButtonDownFcn

From: Frédéric Bergeron

Date: 24 Aug, 2010 17:44:04

Message: 3 of 3

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i50vfn$btd$1@fred.mathworks.com>...
> Hi Matlabers!
>
> I am wondering how I could count how many times I have cliked on an axes, within my ButtonDownFcn. For the moment, I have that:
> (I have figure 'h.fig' that contains two subplots 'h.cp1' and 'h.cp2'). this is my buttondownfcn:
>
> function bdfcn(hObject,eventdata,h)
> counter=0; %initialization of the counter
>
> seltype = get(h.fig,'selectiontype');
> if ( h.cp1==gcbo || h.cp2==gcbo )
> switch seltype
> case 'normal'
> counter=counter+1;
> end
> end
>
> The thing is that my initialization is not well placed, I don't know where I have to put it... like it is now, everytime I call my bdfcn.m function, counter restarts at 0, so I have always counter=1...
>
> If someone could help me, that would be great.
>
> Thanks!
> Camille.

Hey,
Maybe you can try to put your counter as an application data of your figure or your axes.

To do that, in the main function, initialise the counter:
setappdata(h.fig,'axes_counter',0);

Then, in the buttondownfcn:
function buttondownfcn()
counter=getappdata(h.fig,'axes_counter');
%do things...
setappdata(h.fig,'axes_counter',counter);
end

I used that method for a lot of variable in the GUI I'm programming at the moment. It works great, just be carefull to always use the same handle (h.fig in my example) to call and set the counter with getappdata/setappdata.

Fred

Tags for this Thread

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.

Contact us