Write csv file from the results after statistics using ImageJ macro

Background

After some image processing using ImgaeJ macro, I have a ‘Results’ tab which contains 2 columns A and B. Lets’s say I have 50 rows of data.

Analysis goals

Now I want to subtract the value of last row from all other 49 rows above in column B. And delete the value of last row in column A.

After this, I want to write all the values in “.csv” file (column A, B and C with 49 values each).

Challenges

Below is the part of the code, but I’m stuck and don’t know how to write all the calculated values in a csv file.

A = newArray(nResults() - 1);
B = newArray(nResults() - 1);

D = getResult("B", nResults() - 1);
	
for (i = 0; i < nResults() - 2; i++) {
	A[i] = getResult("A", i);
	B[i] = getResult("B", i);
	C[i] = A[i] - D;
}

Looking forward for some help here.

Thank you.

I give you a piece of script which probably gets you unstuck.


ExcelWriteCoords ="C:\\thefile.csv";

//open the csv file to write coordinates into for NIS
f=File.open(ExcelWriteCoords);


   //
for (i=0;i<ncount;i=i+1)
{

// A B  C   D     E     F
//print(f,xlist[i]+","+ylist[i]+",,"+points+",,"+pixelsize);

if(i==0)
{
print(f,xlist[i]+","+ylist[i]+","+zlist[i]+","+ncount+",,"+ps);  
}

else
{
print(f,xlist[i]+","+ylist[i]+","+zlist[i]+"\n");
}
}

//close the excel file
File.close(f)

//close the image original
close("*");
1 Like

I didnt adapt it to your question specifically but I guess you can interpret.

Essentially do a for loop reading each entry in your arrays and print them like shown

Hi Dlite X, thank you for quick reply.

I still find problems in writing the calculated data to csv file. Below is the updated code:

directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";

A = newArray(nResults() - 1);
B = newArray(nResults() - 1);

	D = getResult("B", nResults() - 1);
		
	for (i = 0; i < nResults() - 2; i++) {
		A[i] = getResult("A", i);
		B[i] = getResult("B", i);
		C[i] = A[i] - D;

		if(i==0) {
		print(resultFilename, A[i] + "," + B[i] + "," + C[i]);
	}
	}

Using the ‘print’ command lists the values in Log window instead of writing them in csv file.

Be careful with the f=File.open(ExcelPath);
see below

ExcelFile = "C:/Users/dleger/Desktop/results.csv";
file = File.open(ExcelFile);
for (i = 0; i < 10; i++) {

print(file,i);
}

File.close(file);

`> Blockquote`