MATLAB Answers

0

Delete line plot and plot new one on UI Axes

Asked by Daniel Boateng on 24 May 2019
Latest activity Answered by Geoff Hayes
on 27 May 2019
In appdesigner, I have values from (10 to 100) in a drop down menu. This will be used to create a line plot. If i click on a value in the drop down menu it should plot a line plot on the UI axes. However if another value is clicked, the old one should be deleted and the new value clicked should be the only one on the line. How do I do this please. I tried this but it not working.
Cons_Line = str2double(app.DCValueDropDown.Value);
for i = 1:length(app.DCValueDropDown.Items)
if isempty(get(groot,'CurrentFigure'))
delete(xline(app.UIAxes2,Cons_Line,'--r'))% this is a test to check whether there is an old line plot if yes it should be deleted
end
xline(app.UIAxes2,Cons_Line,'--r') % this sholud plot the new clicked value
end

  4 Comments

Show 1 older comment
No please i do not have any hold in my code.
Daniel - I don't understand what the following code is attempting to do
if isempty(get(groot,'CurrentFigure'))
delete(xline(app.UIAxes2,Cons_Line,'--r'))% this is a test to check whether there is an old line plot if yes it should be deleted
end
The above seems to suggest if there is no figure (the isempty(get(groot,'CurrentFigure'))) then create a new x-line object and then delete it? Is that the intent? As for
delete(xline(app.UIAxes2,Cons_Line,'--r')))
aren't you just creating a new line object only to delete it again? How would that correspond to the existing one? Do you need to save the handle to the xline object that was created previously?
I wanted to use this to check if there is a presence of the line drawn on the plot with this
if isempty(get(groot,'CurrentFigure'))
delete(xline(app.UIAxes2,Cons_Line,'--r'))% this is a test to check whether there is an old line plot if yes it should be deleted
end
if there is then i will delete that line and rather plot the value selected in the dropdown.

Sign in to comment.

Tags

1 Answer

Answer by Geoff Hayes
on 27 May 2019

Daniel - I think I understand your intent...but does the code do what you are expecting it to do? Does get(groot,'CurrentFigure') actually check for the existence of a line or just for existence of the current figure? And won't the other line just create a new line object at the same location (x-coordinate) as the other new line that you are creating? Look at your code
Cons_Line = str2double(app.DCValueDropDown.Value);
for i = 1:length(app.DCValueDropDown.Items)
if isempty(get(groot,'CurrentFigure'))
delete(xline(app.UIAxes2,Cons_Line,'--r'))% this is a test to check whether there is an old line plot if yes it should be deleted
end
xline(app.UIAxes2,Cons_Line,'--r') % this sholud plot the new clicked value
end
You are using Cons_Line to create and delete a line
delete(xline(app.UIAxes2,Cons_Line,'--r'))
and then create again at the same position (!)
xline(app.UIAxes2,Cons_Line,'--r')
I recommend saving the handle (or vertical line object) from the previous time that you created the line and then delete that object before creating the new line.

  0 Comments

Sign in to comment.