How to clear graphical objects

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 Commenti

Adam Danz
Adam Danz il 29 Mag 2019
"the previous data is just overwritten and I can't figure out how to clear the data"
What is "data"? Is it a variable? A plot? What needs cleared?
The data is a plot, more specifically the polyshape that I created. But when I rerun the function (by selecting another object in the dropdown) it just recreates the new polyshape over the old one.
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.
Geoff,
Thank you so much for helping me out with this. I posted a little more information under Adams comment and a snippet of code. Thanks in advance for the help.

Accedi per commentare.

 Risposta accettata

Adam Danz
Adam Danz il 29 Mag 2019
Modificato: Adam Danz il 29 Mag 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().
Alternatively you could toggle the hold() status.
hold(axHandle, 'off')
ployshape()
hold(axHandle, 'on') %if necessary

4 Commenti

Thanks for the response Adam,
Here is a portion of the function code. h is a struct and f is my figure. The thing that continues to cahnge with each selection on the pop up menu is the z variable represented by newval. My issue is that I can't delete newval and remove it from the graph prior to running through the function again when I click a different option on the pop up menu.
For some reason the variables don't save (or at least I am not sure where they are at) in the functions workspace. So I can't clear them at the end of the loop because if I clear them at the end I can't visualize them on the graph. I also can't clear them at the beginning because the variables aren't existant at the beginning of the function. Thanks so much for your help.
h.dd = uicontrol(f,'Style','popupmenu','Position',[20 100 80 250],'Value',1,'String', XNames,'Callback',@Selection);
h.XValues = XValues;
guidata(h.f,h)
function Selection(h,~)
X = readcell('data.xlsx');
XValues = cell2mat(X(:,2:4));
h = guidata(h);
Name = get(h.dd,'Value');
assignin('base','val',Name);
newval_1=XValues(Name,1);
assignin('base','newval',newval_1);
newval_2=XValues(Name,2);
assignin('base','newval',newval_2);
newval_3=XValues(Name,3);
assignin('base','newval',newval_3);
z1 = newval_1;
thing1 = polyshape(X,Y);
[x1,y1] = centroid(thing1);
scatter3(x1,y1,z1)
z2 = newval_2;
thing2 = polyshape(X2,Y2);
[x2,y2] = centroid(thing2);
scatter3(x2,y2,z2)
z3 = newval_3;
thing3 = polyshape(X3,Y3);
[x3,y3] = centroid(thing3);
scatter3(x3,y3,z3)
end
Adam Danz
Adam Danz il 29 Mag 2019
Modificato: Adam Danz il 30 Mag 2019
Hi Isaiah , there's a lot of improvement needed in this code. Please see my comments below.
h.dd = uicontrol(f,'Style','popupmenu','Position',[20 100 80 250],...
'Value',1,'String', XNames,'Callback',@Selection);
h.XValues = XValues;
guidata(h.f,h) % 1) What are we doing here? You're saving 'h' (a structure of handles)
% to a field within 'h'. Why? You probably don't need this line.
function Selection(h,~) % 2) change the name of this 'h' because it's different from
% the 'h' above and that's confusing.
X = readcell('data.xlsx'); % 3) you're reading from the same file every time a
% selection is made from the dropdown menu? Why not
% just read that file once and store its content either
% as a persistent variable or by using guidata().
XValues = cell2mat(X(:,2:4));
h = guidata(h); % 4) Again, not needed.
Name = get(h.dd,'Value'); % 5) Is this the value from the dropdown menu? If so,
% you just need h.Value or get(h,'Value') from the
% function's input 'h'.
assignin('base','val',Name); % 6) What are we doing here? Don't use assignin() or
% eval() or anything that produces dynamic variable
% names. This is a bad idea and it's not needed.
%---------------------------------------------------%
newval_1=XValues(Name,1); %
assignin('base','newval',newval_1); % see #6 % 7) I would replace this
newval_2=XValues(Name,2); % entire Section with the one
assignin('base','newval',newval_2); % see #6 % below.
% ||
newval_3=XValues(Name,3); % ||
assignin('base','newval',newval_3); % see #6 % ||
%---------------------------------------------------% ||
% ||
%---------------------------% \/
z1=XValues(Name,1); % Better yet, just use XValues(Name,#);
z2=XValues(Name,2); % No need to create new vars z1,z2,z3.
z3=XValues(Name,3); %
%---------------------------%
cla(ax) % 8) Add this to clear your axes. You can get the axis handle (ax)
% from guidata()
%z1 = newval_1; % 7b) no longer needed if you do #7
thing1 = polyshape(X,Y); % 9) where does Y come from?
[x1,y1] = centroid(thing1);
scatter3(x1,y1,z1) % 10) I would specify the axis handle like this
% scatter3(ax,x1,y1,z1);
%z2 = newval_2; % see #7b
thing2 = polyshape(X2,Y2); % 11) where does Y2 come from?
[x2,y2] = centroid(thing2);
scatter3(x2,y2,z2) % see #10
%z3 = newval_3; % see #7b
thing3 = polyshape(X3,Y3); % 12) where does Y3 come from?
[x3,y3] = centroid(thing3);
scatter3(x3,y3,z3) % see #10
end
I recommended using cla() which clears the entire axes. However, Geoff Hayes's suggestion to store the object handles is the better approach. It would look something like this:
s1 = scatter3(ax,x1,y1,z1);
s2 = scatter3(ax,x2,y2,z2);
s3 = scatter3(ax,x3,y3,z3);
set(h,'UserData', [s1,s2,s3]); % or h.UserData = [s1,s2,s3]; where h is the handle
% to the dropdown box (first input in your func).
Then at the beginning of your function you can delete those objects like this:
delete(h.UserData) % where h is the handle to your dropdown box
Adam. This information is so valuable. Thank you very much for your help.
Adam Danz
Adam Danz il 30 Mag 2019
I'm glad it was helpful!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by