How to delete an item in ROI manager

No=2
for (j=1;j<(No+1); j=j+1){
	roiManager("Open",  pathbase+"/RoiSet.zip");
	Roiname=symmetry_type+"_"+j; 
	
	roiManager("select",findRoiWithName(Roiname));
	roiManager("Delete");
	roiManager("Save", pathbase+"/RoiSet.zip");
	close('ROI Manager');
}
function findRoiWithName(roiName) { 
	nR = roiManager("Count"); 
 
	for (i=0; i<nR; i++) { 
		roiManager("Select", i); 
		rName = Roi.getName(); 
		if (matches(rName, roiName)) { 
			return i; 
		} 
	} 
	return -1; 
} 

I tried above code but did not work.

Hi,
I do not see an obvious error with the above code. Iterating over the rois while deleting can be tricky, but your code looks alright.

What I like to do is to get all the indices of rois to be deleted in an array first and then delete them all at once. Maybe you could try this approach.

Best regards,
Volker

2 Likes

roiManager(“Select”, 0); //Delete the first ROI
roiManager(“Delete”);

roiManager(“Select”, newArray(0,3)); //Delete ROIs 0 and 3
roiManager(“Delete”);

roiManager(“Delete”);// Deletes all ROIs

Also, these lines should probably be outside the for loop:

The first one before the loop, the other two, after the loop.
Otherwise, you are opening and saving the same roiset in each operation.

Nico