Delete line plot and plot new one on UI Axes

11 views (last 30 days)
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
Geoff Hayes
Geoff Hayes on 27 May 2019
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?
Daniel Boateng
Daniel Boateng on 27 May 2019
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.

Answers (1)

Geoff Hayes
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.

Categories

Find more on Two y-axis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!