Macro only does half of the loop

I have a results table with the majority of the means being 0. I want to remove all the results that have a mean of 0.

for (i=0; i < nResults ; i++) {
meen= (getResult(“Mean”, i));
if (meen==0.00) {
IJ.deleteRows(i,i);
}
}

is my entire code, being ran through Plugins > New > Macro

When I run it, it removes half of the rows that have a mean of 0, I can manually click run 20 times to get the table I want

There must be something I’ve missed, this should be so simple, I’m going crazy trying to figure out what I’ve done wrong

Hi @SamuelLiu
welcome to the forum. I will try to help so you don’t get crazy. :crazy_face:

Try this:

for (i=nResults-1; i >= 0; i--) {
	mean= (getResult(“Mean”, i));
	if (mean==0.00) {
	IJ.deleteRows(i,i);
	}
}

… looping bottom to top.

2 Likes

nResults" is always updated …

Use:

n = nResults;
for (i=0; i < n ; i++) {

Now the issue is that some later rows cannot have their mean evaluated. EG if there were 100 rows, it cannot find row 51 if the previous 50 rows were deleted due to there only being 50 rows left in the results table

Try the following example code:

a = newArray(1,2,0,0,5,0,7,8,0,10,11,12,0,14,0,16,0);
Table.create("Test");
Table.setColumn("Numbers", a);
i = Table.size;
while (i>0) {
   i--;
   if ( Table.get("Numbers", i)==0 ) Table.deleteRows(i, i);
}