Results (analyze particles) saved separately for each ROI

Hi folks,

I’m working on a macro to analyze particles of predefined ROI’s. Say I have ten ROI’s. After running the macro, I get a result table with all the data in one table. However, in need to separate the results based on the ROI. So at the end I would want to have ten excel files, one for each ROI, all within a macro. Is this possible?

Hi @svylol,

the following macro code should achieve this.
You can test it first on an example image and then extend it for all the images in a specified folder if this is desired.


for(i=0; i<roiManager("Count"); i++) {
	roiManager("Select", 0);
	roiName = call("ij.plugin.frame.RoiManager.getName", i);
	
	run("Analyze Particles...", "display");	//exchange this for your Analyze Particles parameter recording
	saveAs("Results", dir + "Results-"+roiName+".tsv");
	selectWindow("Results");
	run("Close");
}
1 Like

Thanks @biovoxxel!!

I think I’m getting close but I am getting an error . Here is what I have

for (i=0 ; i<roiManager(“count”); i++) {
roiManager(“Select”, 0);
roiName = call(“ij.plugin.frame.RoiManager.getName”, i);
selectImage(id);
roiManager(“select”, i);
run(“Analyze Particles…”, “size=0-Infinity circularity=0.00-1.00 show=[Bare Outlines] display “);
saveAs(“Results”, /Users/svyatoslavkleshchev/Desktop/txt + “Results-”+roiName+”.tsv”);
selectWindow(“Results”);
run(“Close”);
}

Below is a screenshot of my error.

Thanks for the help, I appreciate it!

Hi svy, I think I see the error, your directory path needs to be a string. Try this line instead:

saveAs("Results", "/Users/svyatoslavkleshchev/Desktop/txt/" + "Results-"+roiName+".tsv");

Great! Thank you Good Sir.
Question, is there a way to just save the flies in the folder I am working on? I would like to save the flies in a way that I don’t have to change the directory each time (or if another person uses the script, they won’t have to mess with changing the location),

Thanks again

@svylol,

you can call dir = getDirectory("image"); then it will be saved always in the directory where your image is coming from.

Just have a look on the listing of Built-in Macro Functions to get an overview on how to use all the macro commands which are not recordable.

1 Like