Fiji Macro issue – No window with title (...) found

Hello everyone! I’m new to this site as well as to Fiji and more specifically the macro function therein– which my question is about. Please excuse if this question is a little basic or I am not using this site correctly.

Below you will find code which is supposed to – among other things – separate the different slices of a video into images which then are saved into individual files. When I run the code I get an error message saying "No window with title “TILL-00010” found. So it seems the code is fine until it reaches the 10th slide. Would anybody here know what the problem could be? Any help would be appreciated.

Best,
Severin

----------------------------------------------

// Get directory
dir  = getDirectory("Select a Save Directory");
dir2 = replace(dir,"\\","/");

// Ask user which images to keep
Dialog.create("Slice Keeper");
Dialog.addNumber("First:", 1);
Dialog.addNumber("Last:", 5);
Dialog.addNumber("Increment:", 1);
Dialog.show();

// Get dialog numbers user entered
firstVal     = Dialog.getNumber();
lastVal      = Dialog.getNumber();
incrementVal = Dialog.getNumber();

// Stack length
stackLength = lastVal - firstVal + 1
print(stackLength)

// Get currently selectes stack name
stackName = getTitle();
selectWindow(stackName);

// Run the slice keeper
run("Slice Keeper", "first=&firstVal last=&lastVal increment=&incrementVal");

// Create images from saved stack
stackNameKept = getTitle();
run("8-bit");
run("Stack to Images");

// Loop through and save all images and save to desired directory
imgInd = 1;
for (i = 1; i <= stackLength; i++)
{
	// Save Raw data file
	selectWindow("TILL"+(firstVal+i-1));
	if (imgInd < 10) {
		saveAs("Tiff", dir2 + "Raw_0" + (imgInd));
	}
	else {
		saveAs("Tiff", dir2 + "Raw_" + (imgInd));
	}
	imgInd = imgInd + 1;
}

// Close all images
while (nImages>0) { 
  selectImage(nImages); 
  close(); 
}

Hi @Basic_Year
Welcome to the forum.

I haven’t checked your code in total. But the following line is definitely wrong

run(“Slice Keeper”, “first=&firstVal last=&lastVal increment=&incrementVal”);

Change it to

run("Slice Keeper", "first=" + firstVal + " last=" + lastVal + " increment=" + incrementVal);

and try again.

1 Like

Hey @Peter Haub,

Thank you for the quick response! I edited the code using your suggestion but I’m afraid nothing has changed.

It might also be worth mentioning that my slices are automatically named as follows: “Till-0001” even though the File name is Till.avi. So Fiji seems to be adding three zeros to every slice name which might be why the selectWindow(“TILL”+(firstVal+i-1)); function doesn’t kick in?

Here is a little trick to add leading zeros to your image index:

nStr = "" +(firstVal+i-1);
tmpStr = "00000" + nStr;
s = "TILL-" + tmpStr.substring(nStr.length());

print(s);
1 Like

A shorter version to add leading zeros is

idx = firstVal + i - 1;
s = "TILL-" + IJ.pad(idx, 5);
print(s);

Thanks @Wayne for this hint.

1 Like

Thank you @phaub! I’ve tried to implement the code but I am getting the following message:

Undefined variable in line 33

idx=firstVal + - 1 ;

I feel like I’m missing something really obvious. Ive tried to put the code in different places in the code but ones I am past the “Loop through and save all images…” section I’m back at where I started.

Thanks again!

idx = firstVal + - 1;

Something is missing between + and -.

Hey @phaub!

Sorry, I should have proofread my last message! The actual code did have the variable in it and the message I get is still:

Undefined variable in line 33
idx = firstVal + - 1 ;

Here is the code:

// Get directory
dir  = getDirectory("Select a Save Directory");
dir2 = replace(dir,"\\","/");

// Ask user which images to keep
Dialog.create("Slice Keeper");
Dialog.addNumber("First:", 1);
Dialog.addNumber("Last:", 15);
Dialog.addNumber("Increment:", 1);
Dialog.show();

// Get dialog numbers user entered
firstVal     = Dialog.getNumber();
lastVal      = Dialog.getNumber();
incrementVal = Dialog.getNumber();

// Stack length
stackLength = lastVal - firstVal + 1
print(stackLength)

// Get currently selectes stack name
stackName = getTitle();
selectWindow(stackName);

// Run the slice keeper
run("Slice Keeper", "first=" + firstVal + " last=" + lastVal + " increment=" + incrementVal);

// Create images from saved stack
stackNameKept = getTitle();
run("8-bit");
run("Stack to Images");

idx = firstVal + i - 1;
s = "TILL-" + IJ.pad(idx, 5);
print(s);

// Loop through and save all images and save to desired directory
imgInd = 1;
for (i = 1; i <= stackLength; i++)
{
	// Save Raw data file
	selectWindow("TILL" + (firstVal+i-1));
	if (imgInd < 10) {
		saveAs("Tiff", dir2 + "Raw_0" + (imgInd));
	}
	else {
		saveAs("Tiff", dir2 + "Raw_" + (imgInd));
	}
	imgInd = imgInd + 1;
}

// Close all images
while (nImages>0) { 
  selectImage(nImages); 
  close(); 
}

The code to create appropriate files names (with leading zeros) must be inside the loop …
Remove them from outside the loop and use:

for (i = 1; i <= stackLength; i++)
{
	// Save Raw data file
	idx = firstVal + i - 1;
	s = "TILL-" + IJ.pad(idx, 5);
	//print(s);
	selectWindow(s);
	//selectWindow("TILL" + (firstVal+i-1));
	if (imgInd < 10) {
		saveAs("Tiff", dir2 + "Raw_0" + (imgInd));
	}
	else {
		saveAs("Tiff", dir2 + "Raw_" + (imgInd));
	}
	imgInd = imgInd + 1;
}

And re-check the title string as well as the number of leading zeros and adapt IJ.pad(idx, 5) if necessary. Your posts are not clear in terms of number of leading zeros.

1 Like

Thank you so much @phaub! I adapted the IJ.pad(idx,5) to (idx,4) and it worked! Thanks again also to @Wayne!