Position property of Polygon object created in Live Script does not update when moved interactively.

2 views (last 30 days)
I get different results depending on whether I enter commands directly in the command window or in a Live Script. I've described two scenarios below and hope someone knows why they give different results.
In the first scenario, I enter the following commands directly in the command window:
im = imread("peppers.png");
h = imshow(im);
p = drawpolygon(h.Parent, 'position', [100 100; 300 100; 300 200; 100 200]);
The figure appears in a new window as expected. I then interactively move the polygon to a new position within the figure. After moving the polygon, I look at its position using:
p.Position
and get
ans =
276 261
476 261
476 361
276 361
The polygon's position has updated to the location where I moved it.
In the second scenario, I enter the same 3 inital commands to create the figure and polygon in a Live Script and run it. I see the figure in the Live Script and can move the polygon as before. However, when I look at p.Position in the workspace, I get the polygon's original position:
ans =
100 100
300 100
300 200
100 200
(Side note: I'm not sure why the spacing of the two ans values is different. I didn't explicity change any formatting.)
If I look at p.Position in a new section of the Live Script and run that section separately, It also gives the original position, eventhough I can see the polygon is in a different position from where it started.
Please explain what's going on and what to change so that I can use an interactive polygon in a Live Script.
  2 Comments
Zinea
Zinea on 9 Apr 2024
The value of p.Position is changing for me even for the second scenario using Live Script. What MATLAB version are you using?
Steven Martin
Steven Martin on 10 Apr 2024
I had been using R2022b but just updated to R2024a. I get the same result in both releases.
Here's a screen snip of the Live Script tab showing the two sections and their results. The polygon in the image was moved interactively after running the first section but before running the second section. The ans for p.Position is the initial position of the polygon before it was moved.

Sign in to comment.

Accepted Answer

Arun
Arun on 17 Apr 2024
Edited: Arun on 17 Apr 2024
Hi Steven,
I understand that the position of the polygon object is updated in the workspace when the object, created using command window is relocated. However, this does not occur when using Live Script for the same purpose.
This is because the Live Editor is using a cloning mechanism to present its figures. Essentially, when a figure is created within a Live Script, it undergoes a procedure that results in a clone of the original figure being embedded in the Live Script.
For example, if you run the following code in the Live Editor:
f = figure;
ax = axes(f);
plot(ax,1:10)
and zoom/pan the axes, the values of XLim/YLim of the axes stores in the variable ax won't change. This is because the displayed figure is not the figure that was created during the execution, but a copy of that figure.
Similarly, when the user gets the Position property of drawpolygon, this is not the object that was interactively changed but the copy of that object was interactively modified.
The figure outputs in the Live Editor are not directly accessible to the user, but it is still possible to get them and query certain properties if needed, for example:
f = findall(0,'type','figure','Visible','on');
p=findobj(f,'type',"images.roi.polygon"); % assuming there is one visible figure
p.Position % this is the Position value of the modified polygon
For more information related to "Live Scripts and Functions" please refer the share documentation link:
Hope this helps.
  2 Comments
Steven Martin
Steven Martin on 17 Apr 2024
@Arun's answer is confirmed by MATLAB tech support staff. They also suggested that you can find the Live Script's copy of the figure and polygon in order to get its current position or other properties. Their suggested code, which I haven't tried, is:
% Identify the copied figure object
f = findall(0,type='figure',visible ='on');
% Identify the polygon associated with the copied figure
p = findobj(f,type='images.roi.polygon');
% Query the position property of the polygon
p.Position

Sign in to comment.

More Answers (0)

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!