Adding a column with values resulting from a calculation from other cells

Hello there,
I have another question for you experts.
What I want to do:

  1. Duplicate an image (with its ROI, both given) twice and name the duplicates differently.
  2. Threshold one of them, copy the thresholded ROI and paste it on the other one.
  3. Measure (and get other parameters) from this last duplicate’s ROI.
  4. Create a column in the measurements table. Its value is the division of other two values from the table.
  5. Save everything in a new folder in the directory of the original image.

What I have done:

currentImage = getTitle();
dir = getDirectory("image");

//Remove the .tif at the end and add Threshold or EVAL instead 
currentImageLength = lengthOf(currentImage);
currentImage = substring(currentImage, 0, currentImageLength-4); // the -4 gets rid of the .tif
run("Duplicate...", "title="+currentImage +"_Threshold.tif");
Duplicate_Threshold= getTitle();
run("8-bit");													//convert to 8bit
run("Duplicate...", "title="+currentImage +"_EVAL.tif");
Duplicate_EVAL= getTitle();
run("8-bit");													//convert to 8bit

//Threshold for Duplicate_Threshold
selectWindow(Duplicate_Threshold);
setAutoThreshold("Default dark");
run("Threshold...");
run("Threshold...");
setThreshold(107, 255);
setOption("BlackBackground", true);
run("Close")

//Copy the mask and paste it at Duplicate_EVAL
run("Convert to Mask");
run("Copy");
selectWindow(Duplicate_EVAL);
run("Paste");

//Measure parameters
run("Measure");
run("Thickness", "thickness spacing");
run("Volume Fraction", "algorithm=Voxel surface=6 use");

//Calculate  Tb.N
BVdurchTV= getResult("BV/TV", 14);
Tb.Th=getResult("Tb.Th Mean (mm)", 6);
Tb.N= (BV/TV)/Tb.Th;
setResult("Tb.N (1/mm)", 15, Tb.N);

/*saveAs("Tiff", dir + Duplicate_Threshold);
saveAs("Tiff", dir + Duplicate_EVAL);
saveAs("Results", dir + CurrentImage);

My problems/questions:

  1. I get an error in the line 36:
    Row(14) out of range in line 36.
    BVdurchTV= getResult(“BV/TV”, 14<)>;
    The rows’ numbers I have counted manually. I do not know if there is a better way.
    For the calculations I have taken the names of the columns.
  2. I don’t know how to create a folder and save the data there.
  3. I want to run this program with more pictures and save the data in the same file. Will this work or give me some problems?

I want to say that I have really tried before asking you. I have been reading https://imagej.nih.gov/ij/developer/macro/functions.html#setResult and http://imagej.1557.x6.nabble.com/How-to-create-a-customised-results-table-and-add-data-to-it-within-a-macro-td5000701.html but sometimes I get lost. But I am getting better. Slowly :smiley:.
Thank you so much, as always :heart:

Dear @Diego_Hens,

since you specify the row from which you want to get the result by a number, it can quickely happen, that this row might not exist some time when running the macro.
Depending on if the row you want is always the last one in the table or always the pre-last one etc. you can e.g. specify it via nResults

getResults("BV/TV", nResults);

or

getResults("BV/TV", nResults-x);

where x indicates the count backwards or another variable

Furthermore, you will most likely also get an error message regarding your variable Tb.N because of the point. It is discurraging or most likely not allowed to have such a variable. I would the rather name it TbN without any special characters.

I have corrected it as you said, but I am still getting the same error :confused:

Are you aware the the first row starts with 0 and the last row is nResults-1?

forgot to mention this :blush:

No, I didn’t know. I still cannot solve it. I’m sorry :confused:
I have written this:

//Calculate  Tb.N
BVdurchTV= getResult("BV/TV", nResults-1);
TbTh= getResult("Tb.Th Mean (mm)", nResults-9);
TbN= (BV/TV)/TbTh;
setResult("Tb.N (1/mm)", nResults, "TbN");

It still says “row (-7) is out of range” or “row (-8) is out of range)”