clear axes in GUI figure in callback function

8 views (last 30 days)
Hi.
I designed a GUI figure that makes a contour with a text that changes depending on the values determined by sliders.
Here are my questions (sorted by importance):
1. When I run the figure, everything works fine. But if I change the value of a slider, the text displayed from the previous contour remains in the axes, together with the new one. So after changing the slider values a few times, it fills up with text. Here's an excerpt from the code (simplified a little):
contour(handles.Scosts,x1,x1,p,'LevelStep',2,'Fill','on')
hold on
contour(handles.Scosts,x1,x1,p, [34 34], 'Color','k')
...
text(p_b_opt,c_b_opt,pt,'\leftarrow optimal cost');
Is there a way I can get the callback function to clear the axes from the previous contour every time I run it?
2. In the figure, I use many sliders which each execute the same function which accesses all of their values. The code is pretty long. Is there a way I can get the function to run every time I click one of the sliders without having to copy/paste the entire code to each slider's callback function (in case I want to modify it later)? I have tried putting it into a regular function that executes within the callback functions, but that didn't work, because I have to set many slider values [e.g. set(handles.sliderIntRate,'Value',x)], which the function cannot do, because it doesn't recognize the handles.
Thanks in advance for your help!
Marc

Accepted Answer

Image Analyst
Image Analyst on 3 Sep 2013
Try
cla reset;
Make sure you're current axes is the one you want to clear, otherwise pass in the handle of the axes you want to clear to cla() or axes().
  3 Comments
Marc Jakobi
Marc Jakobi on 5 Sep 2013
Edited: Marc Jakobi on 5 Sep 2013
Thanks. When I type "help cla" into the Matlab command window, it tells me to use upper case letters for RESET. Lower case did not work either. I found out what my problem was though.
cla(handles.Scosts)
did the job, as you mentioned in your first answer.
Noah Griffiths
Noah Griffiths on 22 Dec 2020
Alternatively, in the MATLAB GUI, i could only get cla(app.UIAxes, 'reset') to work
So if this doesn't work, which for me, it didn't then use this. (maybe its deprecated, not sure)

Sign in to comment.

More Answers (2)

Marc Jakobi
Marc Jakobi on 4 Sep 2013
Thanks for the reply. It didn't work though. I tried putting
cla(handles.Scosts,'RESET')
at the beginning of the callback function, but I get the following error message:
Error using clo (line 32)
Unknown command option.
Error in cla (line 29)
clo(ax, extra{:});
Error in OnlineTool>PVInvCost_Callback (line 91)
cla(handles.Scosts,'RESET')
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in OnlineTool (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)OnlineTool('PVInvCost_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

Marc Jakobi
Marc Jakobi on 5 Sep 2013
I figured out the solution to my second problem. Just had to set the callback function of all the other sliders to the one of the first slider in the property inspector.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!