I am trying to use the Find Peaks plugin (by Tiago Ferreira @tferr) in a macro, but I am having trouble to get it to run in a (time-) loop. In this macro, I am trying to move a lineROI (with user-defined length and width) in the centered over the brightest front of moving cells, where after I want to save the intensity profile of the line-ROI of 2 other channels in a table. The Macro actually works, but the issue seems to arise when I want to perform ‘plot profile’ within the same loop (please see macro: line 160-166 in macro). The crashes when I put these functions in the loop, but works when I out-comment lines 160-166.

I have traced down the issue to the ‘Find Peak’ plugin: I am using the X1-coodinate of the peak, which I am extracting from the window ‘plot values’ (which opens, but I am not selecting the window). However, to have the loop working, I need to close the ‘plot values’ window, but I am so far not able to select the window in the ImageJ macro language and close it. I read in the image-J forum that one can use the ‘auto-close’ (which I am trying in line 106 – for every loop), but also this does not seem to work for me. Is there any way to either close this window from the macro-language, or to extract the X1 –value?

To be complete, I have uploaded a draft of the macro here (as it is a bit long):

the close() function only applies to images, that’s why is not working for you. From the documentation:

close()
Closes the active image. This function has the advantage of not closing the “Log” or “Results” window when you meant to close the active image. Use run(“Close”) to close non-image windows.

Your macro is extremely long and hard to debug. Please consider splitting into blocks so that a task is executed in a self-contained function. E.g., you could replace your close() calls with your own close function that works with any ImageJ window:

I think your problem is not related to Find Peaks. It is rather that, for some reason,getProfile() is returning an empty array when you call profile = getProfile();. You can test this by including a debug line before your for {} loop:

profile = getProfile();
if (profile.length == 0) {
print("An exception is about to be thrown in the upcoming for loop");
// handle it here
}
for (j=0; j<profile.length; j++) {
// (...)
}

Also, your macro overrides user preferences (e.g., it changes the default settings for new plots) without user permission which is rather impolite. Please use saveSettings() at the beginning and restoreSettings at the end of your macro to avoid overriding user preferences.