Saving values to text file


Hi All,

I just started working with ImageJ and have trouble saving information to a text file.
I’m running a threshold where I set the threshold manually. Then I want to save the lower and upper threshold in an text file.
However it does not give an error, but it is also not saving anything into the textfile.

Hopefully someone can help my to find the mistake in my code.


print("your thresholds are; "+lower, "to "+ upper);"E:\\OMIG\\Iris 161017 - kopie\\Masks\\threshold.txt");
print(file, "Minimal threshold"+lower+"\n");
print(file, "Maximal threshold"+upper);


In the code you need to “escape” the backslash.
E:\\OMIG\\Iris 161017 - kopie\\Masks\\threshold.txt

Your code also will look nicer on the forum web page if you place
on an individual line right above and right below the code.

That is the diagonal Apostrophe next to the 1 on the US-keyboard.
On a German keyboard that may be next to the question mark.


Hi stmfiji,

Your suggestion unfortunately did not work. If I remove the " " in the name, I get an error saying undefined variable.

Do you have any other suggestion about how to save my variables in one textfile?



The “” needs to be in the"");
The does the script below give the desired result?

print("your thresholds are; "+lower, "to "+ upper);      
//"E:\\OMIG\\Iris 161017 - kopie\\Masks\\threshold.txt");
file ="");

print(file, " Minimal threshold "+lower+"\n");
print(file, " Maximal threshold "+upper);

This script points out strange behavior of the print to .txt aspect of the macro language I spent all morning trying to figure work around. Question for anyone who knows,

Why does the “\n” in the print line behave like a “\t”?

The output in the .txt file from the above code looks like this
’Minimal threshold 20106 Maximal threshold 53969’

(when I pasted it in, it is initially in to 2 lines…)

When I replace the “\n” with “\t” in the above code the output in the .txt looks like this
Minimal threshold 20106
Maximal threshold 53969

What kinda of weird metacharacter magic is behind this?


I have some answers from playing around for a few mins just now.

You don’t need the new line “\n” delimiter, it automatically prints to a new line anyway, the below code would print on two separate lines.

print(file, " Minimal threshold "+lower);
print(file, " Maximal threshold "+upper);

It appears that having a space after the “\n” delimiter results in a new line (see below), but without the space it doesn’t.

print(file, " Minimal threshold "+lower+"\n ");
print(file, " Maximal threshold "+upper);

As a note, the “\n” delimiter doesn’t seem to work at all if you add it anywhere in the string. Maybe you can only print a single line of string using this function?


So that "\n" creates a tab instead of a newline? is that the only thing that undoes the newline of each ‘print()’ ?

I ask because I am trying to print elements of different length arrays in distinct rows. That is, I want rows of different lengths to be printed to a .txt file. Using ‘print()’ is problematic because if I loop through each element of an array, each print() instance will create a new line.

I have tried File.append(), this doesn’t seem to use the tabs to keep elements on the same line.

The final result I want is kinda illustrated in this printDemo but I want the elements in each column to be elements from an Array.


I guess you have solved your own problem using the “\n” when it doesn’t work as expected.

If you cycle through a single array on one line using the “\n” [no space] and then use an "\n " [with a space] to create a new line and then cycle for the next array. Something like the code below (it needs a bit more imagination, I’m not sure how you’re making the arrays?).

file ="");

for (i=0; i < 5; i++) {
	array = newArray(i, i+1, i+2, i+3);
	for (x=0; x < array.length; x++) {
		print(file, "string" + array[x] + " \n");
	print(file, "\n ");

Do you think that would work?

How to get XY coordinate of selection line crossing a ROI

Yes, this is along the lines I what would work.

this_dir = getDirectory("Choose a Directory");
path = this_dir + "result.txt";
array = newArray("f","CO1" ,10,15);
for (i=0; i < 5; i++) {
	array = newArray("f",6,40,155, i);	
for (x=0; x < array.length; x++) {
	File.append( array[x] + "\t", path);
File.append("\n ", path);

However, when I open the txt files The new lines change depending on the program (NotePad or Word) and the new line metacharacters are not translated. It doesn’t seem like the data can be imported into Excel when I try to separate by the deliminator character (tab). I would like to come to some format where I can distinguish separate lines for working with the data.