argument gets deleted without a reason (Invalid or deleted object. Error in images.roi​.Freehand/​get.Positi​on )

7 Ansichten (letzte 30 Tage)
לק"י
Hi guys,
i wrote a code and i get sometimes appears an error that an object is invalid or been delted. i went through it several time and couldn't get where is problem. the object is fhroi.Position which is 2 columns vector, with changing x length due to automated sampling and only after the one before it was finished.
Invalid or deleted object.
Error in images.roi.Freehand/get.Position
Error in Voronoi_exclude_borders_02012023_reanalyze_vorpolratio (line 254)
if vorroi(j)==1 && ismember(1, c{j})~=1 && ismember(0, inpolygon(v(c{j},1),v(c{j},2), fhroi.Position(:,1), fhroi.Position(:,2)))~=1
I made flags between the objects first appernce and the error line
%the original code:
if m==1 && reanalyzeolddata==0 %apply free hand tool only once for the first figure of each cells that passed 3 conditions of min max intensity filtering.
fhroi=drawfreehand;
fhroi.FaceAlpha=0;
elseif m~=1 && reanalyzeolddata==0
pltfhx=[fhroi.Position(:,1); fhroi.Position(1,1)];
pltfhy=[fhroi.Position(:,2); fhroi.Position(1,2)];
plot (pltfhx,pltfhy, 'LineWidth', 3, 'Color', [0 0.4470 0.7410])
elseif reanalyzeolddata==1
pltfhx=[analysisdata{9,ranlzcntr}.Position(:,1); analysisdata{9,ranlzcntr}.Position(1,1)];
pltfhy=[analysisdata{9,ranlzcntr}.Position(:,2); analysisdata{9,ranlzcntr}.Position(1,2)];
plot (pltfhx,pltfhy, 'LineWidth', 3, 'Color', [0 0.4470 0.7410])
end
%code with flags
if m==1 && reanalyzeolddata==0 %apply free hand tool only once for the first figure of each cells that that passed 3 conditions of min max intensity filtering.
fhroi=drawfreehand;
fhroi.FaceAlpha=0;
fhroi.Position %flag
1 %flag
elseif m~=1 && reanalyzeolddata==0
pltfhx=[fhroi.Position(:,1); fhroi.Position(1,1)];
pltfhy=[fhroi.Position(:,2); fhroi.Position(1,2)];
plot (pltfhx,pltfhy, 'LineWidth', 3, 'Color', [0 0.4470 0.7410])
fhroi.Position %flag
2 %flag
elseif reanalyzeolddata==1
pltfhx=[analysisdata{9,ranlzcntr}.Position(:,1); analysisdata{9,ranlzcntr}.Position(1,1)];
pltfhy=[analysisdata{9,ranlzcntr}.Position(:,2); analysisdata{9,ranlzcntr}.Position(1,2)];
plot (pltfhx,pltfhy, 'LineWidth', 3, 'Color', [0 0.4470 0.7410])
fhroi.Position %flag
3 %flag
end
What was frustrating, is that after this run I had to alter the code (at line ~350) long after the bug (at line 260) because of unmatched matrices dimensions to only save the fhroi and not altering it at all. infact the object is never altered after accuiring it. when fixed it from %original code and to %fixed code:
%original code:
if needrecroi==1
analysisdata(1:14,ADcntr)={files(i), datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), xbut, ybut, fhroi, currentallcells , v, c, area1};
else
analysisdata(1:13,ADcntr)={files(i), datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), 'no rectangular roi defined', 'no rectangular roi defined', fhroi, currentallcells , v, c, area1};
end
else
area1=[];
if needrecroi==1
analysisdata(1:13,ADcntr)={files(i), ADcntr, datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), xbut, ybut, fhroi, currentallcells , v, c, area1};
else
analysisdata(1:13,ADcntr)={files(i), ADcntr, datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), 'no rectangular roi defined', 'no rectangular roi defined', fhroi, currentallcells , v, c, area1};
%fixed code:
if needrecroi==1
analysisdata(1:14,ADcntr)={files(i), datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), xbut, ybut, fhroi, currentallcells , v, c, area1};
else
analysisdata(1:14,ADcntr)={files(i), datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), 'no rectangular roi defined', 'no rectangular roi defined', fhroi, currentallcells , v, c, area1};
end
else
area1=[];
if needrecroi==1
analysisdata(1:14,ADcntr)={files(i), ADcntr, datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), xbut, ybut, fhroi, currentallcells , v, c, area1};
else
analysisdata(1:14,ADcntr)={files(i), ADcntr, datintfltrdxfind, datintfltrdyfind, m, minint(m), maxint(m), 'no rectangular roi defined', 'no rectangular roi defined', fhroi, currentallcells , v, c, area1};
I got the fhroi deleted bug again. so i undone the changes exactly before the second code fix presented above (%original code) by just cntrl+z and there again the deleted object bug appeard.
and now I just think to myself wth is the problem :|.
I will add that in earlier versions of the code I got this bug, but have been told that it probabbly a corrupted image source that cause it. so I just deleted each image that gave me these fhroi deleted bugs.
the picture I used that gave me this bug was tested at leat ~100 times and suddenly appeard now.
any suggetions?
matlab file of whole ~400 line (many of them are comments) code included.
thanks and I'm sorry for the long post :).
Amit.

Akzeptierte Antwort

Steven Lord
Steven Lord am 5 Jan. 2023
This is not a bug.
By default, when you call plot MATLAB will reset all axes properties (except for Position and Units) and delete all axes children before creating the new plot. See the Tips section of the newplot function's documentation page for more details. [The default value for an axes NextPlot property is 'replace'.] The object created by drawfreehand is a child of the axes.
To change this behavior either change the axes NextPlot property or call the hold function (which will do that change for you.) I can't evaluate this code in MATLAB Answers (since drawfreehand is interactive) but I'll show you the output that I received when I ran each section of code in release R2022b.
ax = axes;
f = drawfreehand; % I then drew a shape
f.Parent == ax
This returned true.
plot(0:10, 0:10)
f
This showed:
f =
handle to deleted Freehand
If I turn hold on before calling plot, the axes retains both the Freehand object and the line plot and f is no longer a deleted Freehand object.
ax = axes;
f = drawfreehand; % I then drew a shape
f.Parent == ax
This still returns true.
hold on
plot(0:10, 0:10)
f
Now this shows:
f =
Freehand with properties:
I'm not going to list the properties, but they are displayed in my MATLAB session.
  3 Kommentare
Steven Lord
Steven Lord am 26 Jan. 2023
they go through the SAME code
Are you certain of that? The first section of code you posted has if / else blocks with three separate branches (and which could permit none of the three branches to be executed), only two of which call plot.
To test my hypothesis that it's your plot calls that are deleting the freehand ROI object, modify your call to drawfreehand temporarily. Add code to set the UserData property to an onCleanup object that displays the stack when the object is deleted. You can put this after the rest of the inputs in your drawfreehand call.
fhroi = drawfreehand(UserData=onCleanup(@dbstack));
Then run your code (or use the plot call below as a proxy) and see if the stack lists the newplot function. I'm betting it will. If you run your code it should show you which plot call is deleting your freehand ROI object. It's possible one of them is running without hold being turned on first.
plot(1:10)
When I tried this in MATLAB Online (which is currently release R2022b Update 3) the stack looked like:
> In onCleanup/delete (line 23)
In clo (line 53)
In cla (line 50)
In newplot>ObserveAxesNextPlot (line 156)
In newplot (line 99)
In matlab.graphics.internal.newplotwrapper (line 11)
Note that the next to last line is a call to newplot.
Amit Ifrach
Amit Ifrach am 7 Feb. 2023
לק"י
Hi Steven,
Thanks for the answer, to be frank, I just used a 'dummy' variable that will hold the drawfreehand.Position values and it worked. I belive what I did is something like you said, but in another way. I appriciate it very much, thank you!
Amit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by