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

2 views (last 30 days)
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.

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Jun 2021
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

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!