How to concatenate the number of a for loop with a string variable?

Tags: #<Tag:0x00007fb87a4bb4c0>


Dear Imagej users,
I would like make a macro for FIJI doing the following things:
I want to open an image stack and a ROI set manually, and then, get its directory and name (lines 1-2 in the code below), make a new directory in the original directory, named “2018feb14cs1sejt1elott” (lines 3-4), and run a for loop to duplicate all ROIs of the ROI set and save them individually (lines 6-13). I would like to save all ROIs with the name of the original stack, with “ROI_1_A_” added to the begining. The number in the middle (in this example “1”) is always the number of the ROI. For example, the third ROI of the stack named “xxx” should look like this: "ROI_3_A_xxx"
My problem is, that I can not concatenate the number of the for loop with the title. It says, that "undefined variable in row 9 (which is actually row 9)
In an earlier version this row was like: ujname= i + name; In this form, the value of the variable “newname” was NaN (I printed it, that’s how I know it).
My current code is:

1 dir = getDirectory("image");
2 name=getTitle;
3 newdir=dir + "/2018feb14cs1sejt1elott/";
4 File.makeDirectory(newdir); 
5 for (i=0; i<roiManager("count"); ++i) {
6	roiManager("Select", i);
7	run("Duplicate...", "duplicate");  
8	newname= “ROI_” + i + “A_” + name;
9	path = newdir + newname; 
10         saveAs("Tiff", path+".tif");
11        close();
12        selectWindow(name); 
13 }

I can not programme, so my description may not be technically correct. Any answers are welcommed!

Kind regards


Try using the d2s() function on your number to convert it to a string so you can concatenate with other strings. d2s means double (type of number) to string conversion.

Alternatively, maybe change your for loop definition from ++i to i++

EDIT: both of these suggestions were not a problem (see post below)


Dear @Leptopelis,

it’s also worth checking if your quotation marks are correct in line 8: they don’t look like straight quotation marks but instead look like curly ones. That could make all the difference in this case, since the following macro runs just fine for me:

name = "xxx";
for (i=0; i<5; ++i) {
	newname= "ROI_" + i + "A_" + name; // mind the quotes



Thanks for both of you for the answers. The quotion marks were wrong, they caused the problems. The old quotion marks were smaller and the characters inside them were brown. After changing them for the regular, larger ones, the characters became pinkish-red and the macro worked. What are the smaller ones for and what is their name?


I am no expert in typography, but seems pretty sound to me.


Thank you! I will take care of my quotes in the future :slight_smile: