Automatically save both X and Y values for line profile of a z-stack


Hello Friends,

I need to find line profile for a z-stack. I draw a new line manually for each z-slice as the co-ordinates of the line have to be decided on the fly. I need both x (distance along the line) and y (intensity) values from the profile. This can be obtained by clicking on the List button in the plot-profile window. However, I need to automate the process of saving such a list one by one for all images in the stack. I wrote the following macro:

macro “Stack profile Plot” {

if (nSlices==1)   exit("Stack required");
stack1 = getImageID;

n = nSlices;
for (i=8; i<12; i++) {    // loop for the z-slices
    showProgress(i, n);
    run("Clear Results");
    run("Plot Profile");
Plot.getValues(x, y);
    lt = x.length;
    for (j=0; j<lt; j++)
             {setResult("Plot Values", x[j], y[j]);
             print(x[i], y[i]);}
    path = getDirectory("current")+"EMS_free_profile_slice"+ i + ".csv";
    saveAs("Results", path);


This macro runs without error however saves only the Y i.e. intensity values in the .csv files. All the x (distance) values are lost!! Can you please tell me how to fix this issue?

Thank you!


Hi @Frodo,

A quick glance suggests that you don’t add the x values to the results table to save.

 setResult("Column", row, value)

Your current macro adds the y values, in rows indexed by the x values. I would suggest adding a new line:

setResult("Distance", j, x[j]);




Good day,

not perfectly sure what you need beyond the x, y values.

Here is a solution, provided you don’t need a results table:

run( "Plot Profile" );
Plot.getValues( x, y );
str = "";
for ( i = 0; i < x.length; i++ ) { str +=  "" + x[i] + "\t" + y[i] + "\n"; }
File.saveString( str, getDirectory("current")+"EMS_free_profile_slice" + 1 + ".txt" );

Data is saved as a tab-delimited text file. Change “\t” to “,” to save as comma-delimited text file.




Hello Herbie,

Your solution solved my problem! Thanks a lot!!