Matlab opens new digit in callback despite hold
I am creating a GUI with MATLAB GUIDE. Let's say a GUI consists of an axis called axis1
and a slider called slider1
. Next, I'll say that I wanted to do something (for example, a box) in axis1
and change the height of the window using the slider.
I tried to do it by adding a listener to the slider like this:
hListener = addlistener(handles.slider1,'Value','PostSet',@(src,evnt)boxHeightChange(src,evnt, handles.figure1));
in the function of opening the GUI. I also defined:
function boxHeightChange(src, event, figname)
handles = guidata(figname);
% delete "old" box
delete(handles.plottedHandle);
% bring axis in focus
axes(handles.axes1);
% plot the new box (with changed size)
hold on; boxHandle = plotTheBox(event.AffectedObject.Value); hold off
handles.plottedHandle = boxHandle;
% update saved values
guidata(figname, handles);
end
This works, but it always opens a new shape to draw a resizable window instead of drawing to handles.axes1
. I don't understand why as I am calling axes(handles.axes1);
and hold on;
Any idea that might explain the behavior?
source to share
I will post a solution to my question.
Apparently, the listener callback is not declared as a "GUI callback", so the GUI cannot be accessed from boxHeightChange
unless the "command line availability" GUI option is set to "On".
This means: In the GUIDE, go to Tools → GUI Options and set the Command Line Accessibility to On.
source to share
Most of the plotting functions allow you to pass a pair of name values 'Parent', ah
where ah
specifies the axes to plot. I think this is the best way to handle your problem. Your actual plotting command seems to be wrapped in a function plotTheBox
, so you'll have to pass in a handle to the axes somehow.
Your plot command will look something like this:
plot(a,'Parent',handles.axes1)
You solved the problem differently, but I think you should do it your own way, because it's more explicit and less likely to lead to unexpected problems.
source to share