setResult is blank

imagej

#1

I am tearing my hair out over this. I simply want to add a column to my results table with the filename in it so that i can link the measurement to the filename for analysis. The code below runs ok and writes the output files but the “label” column is blank and there is a final error message about test.xlsx being unsupported file.

dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
setBatchMode(true);

for (i=0; i<list.length; i++) {
 showProgress(i+1, list.length);
 open(dir1+list[i]);
 title = getTitle;
 setResult("Label", nResults, title);
 run("Create Selection");
 run("Measure");
 getResult("Area");
updateResults();
saveAs("Results","D://test/"+getTitle+".xls");
 close();
}

#2

Not sure I understand your code, but shouldn’t this be
setResult("Label", i, title);
nResulst is the number of results in the table.
I suppose that you want to put the string in the nth, row?


#3

Good day Gabriel,

works for me and has always worked for me:

mx = 10;
for ( i=0; i<mx; i++ ) { 
   str = d2s(i+0.1*(mx-i-1),1);
   setResult("Label", nResults, str);
}

resultsTable

Best

Herbie


#4

Good day,

your macro code assumes several things:

  1. The images you are processing are really binary!
    Otherwise “run(“Create Selection”);” won’t work.
  2. “D://test/” is a valid directory.
    You don’t actually use “dir2”, do you?
  3. In Set Measuremenst “Area” is checked.
  4. At the end of your macro you need to include the line “setBatchMode(false);”.
  5. There may be more problems though.

Please learn how to correctly write ImageJ-macros and how to use the provided functions:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html

Regards

Herbie


#5

Hm… I see. Thanks.


#6

Hi Andy!

Maybe you didn’t notice, but you can include the image title as a result column in Analyze > Set Measurement… under “Display Label”. It adds a “Label” column just beside the line numbering.

If you perform a measurement within a named Roi (either stored in RoiManager or just manually named), it outputs with following format: [FileName]:[RoiName]

You can ensure this behavior by default in your macro by adding a line setting the desired measurements and including the display parameter, like in:

run("Set Measurements...", "area mean display redirect=None decimal=3");

The easiest way to do it is just to launch the recorder, set these parameters, and then copy the output to your macro.

Cheers!
Nico


#7

Thanks for all the help. I eventually solved it by incorporating some lines from a macro for measurement which was online. I think maybe the lines which helped are:

    if (getVersion>="1.40e")
        setOption("display labels", true);

I will take some time to learn the macro language properly before my next project!