How to clear graphical objects
14 views (last 30 days)
Show older comments
I have a local function inside of my script that runs a gui (not GUIDE, just uicontrol in a figure) and creates a polyshape based on the selection I make in a drop down menu (which references a spreadhseet of values). My issue is that everytime I click the drop-down to select a different row of data, the previous data is just overwritten and I can't figure out how to clear the data iteratively on each pass of the function after clicking the drop-down.
I'd like to be able to view the graph that I create without clearing it automatically. I tried to clear the variables that assign the polyshape but that doesnt work because its inside the local function. So once it runs it can't remove those variables because the caller workspace is reset (thats my understanding of it). Do I need to use a nested function to query mouseclicks? Thanks in advance
4 Comments
Geoff Hayes
on 29 May 2019
Isaiah - save the handle to the polyshape object and then delete it just prior to creating the new shape. You may need to post some of your code so that we can see its structure, but if the callback is nested within the main function then you should be able to do this. For example, you might have
function myGui
% create the GUI
% assign the callbacks
% define the handle to the polyshape
hPolyshape = [];
% in your drop down callback
function dropDownCallback(~,~)
% delete the old polyshape
if ~isempty(hPolyshape)
delete(hPolyshape);
hPolyshape = [];
end
% plot the polyshape
hPolyshape = polyshape(...);
end
end
The above is very rough and will not compile but it gives you an idea of what you can perhaps do.
Accepted Answer
Adam Danz
on 29 May 2019
Edited: Adam Danz
on 29 May 2019
Prior to drawing the updated ployshape, clear the axes using cla() and always specify the axis handle.
cla(axHandle);
ployshape();
If the axis handle is not already available in the function's workspace or in the "handles" input to the callback function, you can get the axis handle by passing it into the plotting function or using findobj() or findall().
hold(axHandle, 'off')
ployshape()
hold(axHandle, 'on') %if necessary
4 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!