Array2[i]=Array1[i]-1 not working

Hello!
I am very new to ImageJ and this is my first time writing a macro on my own. I am trying to write a macro to help me analyse photoquadrats of marine benthic habitats. Briefly put, I have created a grid formed with several squared ROIs (let’s say 4 here) that are labelled with a number (here 1, 2, 3 and 4) and that needs to be characterised based on what is seen on the photoquadrat.
I have a dialog box where the user is ask to choose a category (from dialog.addChoice) and to type the number of all the cells (separated with commas) he wishes to characterised.
In order to attribute a category to each ROI I need to have their index so I wanted to create a new array that would be something like SelectedIndex[i]=SelectedCell[i]-1

The problem is that it works perfectly if I define the array myself (see below):

setOption("ExpandableArrays",true);
Cells=newArray(5,4,7);
CellsIndex=newArray;
for(i=0;i<Cells.length;i++){
	CellsIndex[i]=Cells[i]-1;
}
Array.show(CellsIndex); 

but when I want to use this with the output of my dialog box, I get an error everytime.

setOption("ExpandableArrays",true);

//Selections of the cells
Dialog.create("Cells selection");
Dialog.addMessage("Please select the cells you wish to categorise.");
Dialog.addString("Cells:",""); //for example type 1,3
Dialog.show();
CellsNumber=Dialog.getString();
Cells=split(CellsNumber,",");
Array.show(Cells);

//Save selected cells index
setOption("ExpandableArrays",true);
CellsIndex=newArray;
for(i=0;i<Cells.length;i++){
	CellsIndex[i]=Cells[i]-1;
}
Array.show(CellsIndex);

Below is what the error looks like. I don’t understand where it comes from as the “;” is not missing at the end of my line. I guess the problem is from the way I define ‘Cells’ from the dialog output but I’m not sure what I should do to solve this.
image

If anyone has an idea where this error is from I would greatly appreciate some help !
Thank you !

Hi @Ade

The problem is that when you take input from a dialog box, it’s considered a string. You need to use something like parseInt() to convert Cells[i] into a number before trying to subtract one from it.

For example, this works:

for(i=0;i<Cells.length;i++){
	tmp=parseInt(Cells[i]);
	CellsIndex[i]=tmp-1;
}

This also explains why when you provide the array directly, it works: it’s already a number.

The error you get is because it’s expecting a line end after Cells[i] (look at the <-> part of the error message).

Hope that helps!

2 Likes

Hello @dnmason !
It works perfectly! I had figured the issue was with Cells and the way it was saved from the dialog output but I didn’t know how to fix it… Thank you for you time and for your help !
Ade

1 Like