Delete line plot and plot new one on UI Axes

조회 수: 12 (최근 30일)
Daniel Boateng
Daniel Boateng 2019년 5월 24일
답변: Geoff Hayes 2019년 5월 27일
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
Geoff Hayes
Geoff Hayes 2019년 5월 27일
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 2019년 5월 27일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Geoff Hayes
Geoff Hayes 2019년 5월 27일
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.

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by