Callback within a callback function?

5 visualizaciones (últimos 30 días)
Soumyatha Gavvala
Soumyatha Gavvala el 27 de Jun. de 2016
Comentada: Guillaume el 29 de Jun. de 2016
Hi,
So I have a buttondownfcn callback on my figure 1 and when i click on the figure it runs the function say
set(gca,'ButtonDownFcn',@callback1);
Callback1 generates another figure and I want to be able to have a callback function for the second figure callback2.
set(gca,'ButtonDownFcn',@callback2);
in my first callback doesn't work.
How can I do this?

Respuesta aceptada

Soumyatha Gavvala
Soumyatha Gavvala el 28 de Jun. de 2016
I used this line to get the handle for the axes of the figure
handle= findobj(gcf,'type','axes');
and then used this in
set(handle,'ButtonDownFcn',@Smycallback2);
This worked for me.
  1 comentario
Guillaume
Guillaume el 29 de Jun. de 2016
Well, as you've been told several times. Using gcf, or gca instead of explicitly keeping track of the axis and figure you've created is extremely risky.
It may work under casual testing but will fail horribly when somebody else uses it, simply because they'll have created another figure in the meantime, so gcf will pick up that figure instead of your gui figure.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 27 de Jun. de 2016
I recommend that you avoid using gca in your functions. Doing so leaves you at the mercy of anyone who can change which axes is current (which may include anyone who has control over the mouse.) Obtain the handle of the axes whose property you want to change from the handles structure and explicitly use that handle in your code to ensure you're modifying the property of the axes you think you are.
  5 comentarios
Adam
Adam el 28 de Jun. de 2016
Or create an explicit axes in the figure:
hFig = figure;
hAxes = axes( hFig );
hlines = plot( hAxes,... );
so that you plot onto an explicit axes which is always best. Leaving plot to decide for itself which axes to plot on is always risky - it will do so based on whatever is in focus which, within code, may not be what you think it is, which is the essence of Steven's original answer with respect to gca.
Soumyatha Gavvala
Soumyatha Gavvala el 28 de Jun. de 2016
I posted the answer that I figured out. Thanks for your help.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by