delete line plot via handles not working

5 ビュー (過去 30 日間)
Jason
Jason 2014 年 11 月 28 日
コメント済み: Daniel Melendrez 2019 年 12 月 6 日
Hi, I am plotting a histogram and want to draw a line that represents the mode:
bar(x,counts,'b','EdgeColor','b');
xm=mode(IM(:));
Now as I will want to replot this linen with a slider callback later, and wont want to have to replot the barchart, I thought adding the line plot here using handles, would then allow me to delete it later and replot it (whilst keeping the barchart still present)
So to plot the line using handles (on my axes component)
AxesH = axes(handles.axes5);
LineH=plot([xm xm],[0 max(counts)],'r', 'LineWidth',1); %draw line representing the mode (most frequent)
set(AxesH, 'UserData', LineH); %save so can recall or delete later
guidata(hObject, handles);
Then under a slider callback, firstly I want to delete the current line:
OldLineH = get(handles.axes5, 'UserData');
delete(OldLineH);
and then plot the new one based on the slider value
LineH=plot([x(p) x(p)],[0 max(counts)],'r', 'LineWidth',1);
guidata(hObject, handles);
drawnow;
hold on;
But its not deleting the first one, so I get an accumulation of lines.
  2 件のコメント
Jason
Jason 2014 年 11 月 28 日
編集済み: Jason 2014 年 11 月 28 日
Think I have sorted it. On the slider callback, I also need to add the line to the handles structure so it can delete it next time round:
AxesH = handles.axes5;
LineH=plot([x(p) x(p)],[0 max(counts)],'r', 'LineWidth',1);
set(AxesH, 'UserData', LineH);
guidata(hObject, handles);
drawnow;
Daniel Melendrez
Daniel Melendrez 2019 年 12 月 6 日
Your solution saved my life!
Kudos
Daniel

サインインしてコメントする。

採用された回答

Orion
Orion 2014 年 11 月 28 日
編集済み: Orion 2014 年 11 月 28 日
Hi,
I think this should work
axes(handles.axes5);
if ~isfield(handles,'LineH')
% create the field the first time
handles.LineH = plot([x(p) x(p)],[0 max(counts)],'r', 'LineWidth',1);
else
% update the already existing plot
set(handles.LineH,'XData',[x(p) x(p)],'YData',[0 max(counts)]);
end
guidata(hObject, handles);
drawnow;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by