How to select and analyse specific slices from a Z-stack

I’m looking write a macro that will analyse cells at the ‘top’, center and ‘bottom’ of my sample. I was thinking to do this by selecting a slice in my z-stack that was close to the start of stack as the top (e.g. 10 slices in, as sample is inverted) then the center slice and then close to the end (e.g. 10 slices away from the end of the stack).
My way of going about this was to create a substack of just one slice from each of these positions using the code run(“Make Substack…”, “slices=__”) so

E.g. run(“Make Substack…”, “slices=10”); would be the slice I analyse as top of my sample.

The command nSlices tells you how you how many slices there are in your stack.
I was thinking I could make a variables e.g. centerSlice= nSlices/2 and bottomSlice= nSlices - 10
and use these in the code
e.g. run(“Make Substack…”, “slices= “centerSlice””); and this would pick out the slice in the middle of the stack for me to analyse on its own.

This isn’t working for two reasons:

  1. This code run(“Make Substack…”, " slices= "); will not accept my variable name as the input after “slices= ____”.
  2. Depending on the stack when dividing it by 2 it might not be a whole number. The code run(“Make Substack…”, “slices=___” will not work with a fraction.

If anyone has any suggestions about how I can alter the above code so it works or go about the task in a different way I would really appreciate the help

Thanks :slight_smile:

Ciara

Hi Ciara,

If you want to pass a variable as an argument to a function, you can use string concatenation:
So, if targetSlice holds the position of your slice, you could use :

//round() rounds the number to the nearest integer, so round(nSlices*0.5) sets the middle slice of a stack)
target=round(nSlices*0.5);  
run("Make Substack...", "  slices="+targetSlice);

According to the documention, it should also work to simply precede the variable name that you want to pass as a parameter with & (see https://imagej.net/macros/ArgumentPassingDemo.txt). However, this doesn’t appear to work for ‘Make Substack’. Does anybody know why this doesn’t appear to work?

Finally, do you actually need to make a substack? Rather than creating substacks, you could just select the slice of interest (setSlice(), Built-in Macro Functions) and do your analysis/measurements on that slice. The currently shown slice can also be duplicated with

target=round(nSlices*0.5);  
setSlice(target);
run("Duplicate...", " ");

Hope this helps,
Volko

Hi Volko,

Thank you so much. using SetSlice and duplicate is a much simpler way of doing this task. I am still learning imageJ (an coding in general) so was not aware of the round function or that to pass a variable as an argument to a function you need to use “+varibleyouarepassing+”. I had seen this in other scripts but was not sure what it was for.

Thanks again for teaching me these tips :slight_smile:
Ciara