Callback within a callback function?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Steven Lord
2016-6-27
0 个投票
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 个评论
Image Analyst
2016-6-28
I guess like this:
In callback 1:
hFig2 = figure;
set(hFig2,'ButtonDownFcn',@callback2);
Soumyatha Gavvala
2016-6-28
Guillaume
2016-6-28
The parent of the lines objects returned by plot is the axis hangle:
hlines = plot(...);
haxis = hlines(1).Parent; %index hlines in case there is more than one line
Adam
2016-6-28
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
2016-6-28
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!