Save the value of a variable with 'set measurements'


When using run(“Set Measurements…”, etc…, one can measure variables such as the area of ROIs.

Is there any way to create a macro to additionally save the values of pre-set variables (e.g. ‘x’) alongside those measurements of area?

Thanks in advance!!

Hi @sam1,

If you are using the macro language, the setResult command will probably do what you need.

e.g. this macro opens a sample image, measures it, and adds a column “my_x” in the last row of the Results table and sets the result to 5:

run("Dot Blot (7K)");
setResult("my_x",nResults-1,5); // sets the value in the last line of the results table
updateResults(); // updates the results table to show the new value

Be sure not to overwrite an existing column with your data – if you have “Centroid” selected in Analyze > Set measurements, there will already be a column called “X.” If you don’t use that measurement then there should be no problem.

Hope this helps.

1 Like

I am not sure if I understand correctly what you mean, but here is a demo macro that appends some user set or macro encoded variables like e.g. rolling ball radius of background subtraction or sigma value of Gauss filter to the results table before saving it.
The macro uses a detour via the log window. Possibly there are also more elegant solutions thinkable …

 * this macro saves the results window indirectly,
 * via an intermediate step where the results are
 * copied to the log window and variables are added to the window. 
 * Martin Höhne, May 2017

	//fix variables
	macroname = "_demo_macro.ijm";
	rolling_ball_rad = 30;
	//user defined
	Dialog.create("Example Dialog");
    Dialog.addNumber("Gauss Sigma:", 2);
    Dialog.addNumber("Minimal particel size:", 1);;
  	Gauss_sigma = Dialog.getNumber();
  	min_size = Dialog.getNumber();
//write variables to string-variable "variables" to add them later to the results summary
variables="\n\nVariables:\nrolling ball radius\t"+rolling_ball_rad
			+"\nGaussian blur sigma\t"+Gauss_sigma
			+"\nMinimal particel size\t"+min_size
			+"\nImageJ Version\t"+getVersion;

//open, process and measure the image
run("Clear Results");

run("Cell Colony (31K)");
run("Subtract Background...", "rolling=&rolling_ball_rad light");
run("Gaussian Blur...", "sigma=&Gauss_sigma");
run("Make Binary");
run("Analyze Particles...", "size=&min_size-Infinity display include");
//append the variables to the results table. Use the Log window as an intermediate string buffer
	String.copyResults;//copy results to clipboard
	resultati=String.paste;//clipboard to variable "resultati"
	//--add Variables to end of results
		resultati = resultati+variables;
	print("\\Clear");// erases the Log window
	print(resultati);// writes the contents of the "resultati" variable to the log window

//save results	
	saveAs("txt", homedir+File.separator+"results.xls");

exit("results have been saved to "+homedir+File.separator+"results.xls");