From: Frédéric Bergeron on
Hi,

I'm trying to do a GUI where a line is plotted for every 2 clicks of the user. The program saves the location of the first click (with setappdata), then the second click is saved too and a line is plotted between those two clicks.

I have tried to do an IF loop to see the parity of the number of clicks of the user, but my program just seem to plot the first line between the first two clicks and then ignore the other clicks. Can somebody help me and tell me where I am wrong?
My code is below.

Thank you and forgive my english mistakes, I am french.

function line_gui

figure('MenuBar','none','Name','ligne_gui',...
'doublebuffer','on','NumberTitle','off',...
'Position',[200,200,600,500]);

Axes=axes('XLim',[0,1],'YLim',[0,1]);
set(Axes,'ButtonDownFcn',@ButtonDown);

number_of_click=0;
setappdata(gcf,'nclick',number_of_click);


function ButtonDown(varargin)

number_of_click=getappdata(gcbf,'nclick');

if rem(number_of_click,2)==0
disp('First click');
p_1=get(gcbf,'CurrentPoint');
setappdata(gcbf,'first_point',p_1);

number_of_click=number_of_click+1;
setappdata(gcf,'nclick',number_of_click);

elseif rem(number_of_click,2)==1
disp('2nd click');
p_1=getappdata(gcbf,'first_point');
p_2=get(gcbf,'CurrentPoint');
X=[p_1(1,1) p_2(1,1)];
Y=[p_1(1,2) p_2(1,2)];

number_of_click=number_of_click+1;
setappdata(gcf,'nclick',number_of_click);

plot(X,Y);
hold on

end
From: Frédéric Bergeron on
Anybody have an idea for my gui? Thanks!
From: Walter Roberson on
Frédéric Bergeron wrote:

> I have tried to do an IF loop to see the parity of the number of clicks
> of the user, but my program just seem to plot the first line between the
> first two clicks and then ignore the other clicks.

> function ButtonDown(varargin)
> number_of_click=getappdata(gcbf,'nclick');
> if rem(number_of_click,2)==0
> disp('First click');
> p_1=get(gcbf,'CurrentPoint');
> setappdata(gcbf,'first_point',p_1);
> number_of_click=number_of_click+1;
> setappdata(gcf,'nclick',number_of_click);
> elseif rem(number_of_click,2)==1
> disp('2nd click');
> p_1=getappdata(gcbf,'first_point');
> p_2=get(gcbf,'CurrentPoint');
> X=[p_1(1,1) p_2(1,1)];
> Y=[p_1(1,2) p_2(1,2)];
>
> number_of_click=number_of_click+1;
> setappdata(gcf,'nclick',number_of_click);
> plot(X,Y);
> hold on
>
> end

doc figure_props

NextPlot

replace — Reset all figure properties except Position to their defaults and
delete all figure children before displaying graphics (equivalent to clf reset).

replacechildren — Remove all child objects, but do not reset figure properties
(equivalent to clf).

That is, because you do not have 'hold on' at the time you do the first plot,
you are probably triggering a reset of the figure, thus causing the axes to be
deleted, or at the very least the axes button down callback to be reset.
From: Frédéric Bergeron on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hujatk$fuq$1(a)canopus.cc.umanitoba.ca>...
> Frédéric Bergeron wrote:
>
> > I have tried to do an IF loop to see the parity of the number of clicks
> > of the user, but my program just seem to plot the first line between the
> > first two clicks and then ignore the other clicks.
>
> > function ButtonDown(varargin)
> > number_of_click=getappdata(gcbf,'nclick');
> > if rem(number_of_click,2)==0
> > disp('First click');
> > p_1=get(gcbf,'CurrentPoint');
> > setappdata(gcbf,'first_point',p_1);
> > number_of_click=number_of_click+1;
> > setappdata(gcf,'nclick',number_of_click);
> > elseif rem(number_of_click,2)==1
> > disp('2nd click');
> > p_1=getappdata(gcbf,'first_point');
> > p_2=get(gcbf,'CurrentPoint');
> > X=[p_1(1,1) p_2(1,1)];
> > Y=[p_1(1,2) p_2(1,2)];
> >
> > number_of_click=number_of_click+1;
> > setappdata(gcf,'nclick',number_of_click);
> > plot(X,Y);
> > hold on
> >
> > end
>
> doc figure_props
>
> NextPlot
>
> replace &#8212; Reset all figure properties except Position to their defaults and
> delete all figure children before displaying graphics (equivalent to clf reset).
>
> replacechildren &#8212; Remove all child objects, but do not reset figure properties
> (equivalent to clf).
>
> That is, because you do not have 'hold on' at the time you do the first plot,
> you are probably triggering a reset of the figure, thus causing the axes to be
> deleted, or at the very least the axes button down callback to be reset.


Thank you,

For anyone wondering,
I used the property NextPlot and edited it just after creating the axes in the main function (see below). After some work with the units and the scales of my graph, I can now draw multiple lines exactly where I click.

set(gcf,'Nextplot','add')