How to delete the dotted plot on top of the Image in UIAxes

1 回表示 (過去 30 日間)
Chandrashekar D S
Chandrashekar D S 2021 年 6 月 23 日
コメント済み: Chandrashekar D S 2021 年 6 月 24 日
Hello,
I have plotted a rectangle lines(dots) in UIaxes on top of the image using the selected co-ordinate.
Each time i clicked a button, I am selecting a new coordinates and plotting new rectangle area.
I want to delete the old rectangle on the image. Image should be still present on the UIaxes.
below is the following code i have used to draw the rectangle area on the image in UIaxes
%% Image is already uploded to UIaxes
%% deleting previously selected area
hold(app.UIAxesImage,'on');
h=findobj('Color','r');
delete(h);
hold(app.UIAxesImage,'off');
%% Obviously the above code didnt delete the previously drawn line as expected
%selecting starting point of area
[clickedX, clickedY] = ginput(1);
app.XdirEditField.Value=round(clickedX);
app.YdirEditField.Value=round(clickedY);
%% ploting on the image
Y1=app.YdirEditField.Value+800;
X1=app.XdirEditField.Value+1000;
side1=app.XdirEditField.Value:1:X1;
side2=app.YdirEditField.Value:1:Y1;
hold(app.UIAxesImage,'on');
plot(app.UIAxesImage,side1,app.YdirEditField.Value,'r.',...
side1,Y1,'r.',X1,side2,'r.',app.XdirEditField.Value,side2,'r.');
hold(app.UIAxesImage,'off');
Each time its drawing a new area along with the old.
Please let me know How to achieve this.

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 6 月 23 日
Your current code is drawing a dot at every interger increment in X and Y. That along with your plotting syntax is creating a rectangle made up of thousands of lines.
Try using a different syntax for your findobj command.
h = findobj(app.UIAxesImage,'Type','Line');
delete(h)
You can make this a single line by creating a vector of your X and Y points
X = [side1'; ones(size(side2'))*X1; flipud(side1'); ones(size(side2'))*app.XdirEditField.Value];
Y = [ones(size(side1'))*app.YdirEditField.Value; side2'; ones(size(side1'))*Y1; flipud(side2')];
plot(X,Y,'r.');
If you don't need a dot at every integer, then just use the vertices
X = [app.XdirEditField.Value X1 X1 app.XdirEditField.Value app.XdirEditField.Value];
Y = [app.YdirEditField.Value app.YdirEditField.Value Y1 Y1 app.YdirEditField.Value];
plot(X,Y,'r:','LineWidth',2);
Of course, if you are going to do that, you might as well use rectangle
rectangle('Position',[app.XdirEditField.Value app.YdirEditField.Value X1 Y1],...
'LineStyle',':','EdgeColor','r','LineWidth',2)
% use 'Type','Rectangle' with findobj to delete this object
  1 件のコメント
Chandrashekar D S
Chandrashekar D S 2021 年 6 月 24 日
Thanks you so much @Cris LaPierre, rectangle thing is worked like a charm.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by