Copy the last result into the clipboard

imagej
macro

#1

I’m a newbie for ImageJ macro and my code works not perfectly. I want to copy the last measurement directly into the system clipboard. My current method is to clear all the previous measurements first, then measure, then copy the whole Area column. Yet, sometimes I still need the previous measurements as a double check. The following is my code so far:

run("Clear Results");
run("Measure");
String.resetBuffer; 
    for (i=0; i<nResults; i++) 
        String.append(getResult("Area", i) + "\n"); 
    String.copy(String.buffer);

Is there a way to solve this issue? Can I only copy the last Area measurement into the clipboard while still maintaining the previous measurements?

Additionally, is there any website that has a list of all the commands and respective syntexts for ImageJ macro? That would be super helpful for me to learn how to write the code.

Thank you very much!


#2

Dear @Fijipuff,

I assume you are referring to the last row of a results table? If so, you have to replace the for-loop in your macro s.t. you only copy the last entry to the clipboard:

// Prepare
run("Clear Results");
String.resetBuffer; 

// Measure
run("Measure");

// Copy to clipboard
String.append(getResult("Area", nResults)); 
String.copy(String.buffer);

Best,
Stefan


#3

Thank you for your reply! I tried your code, but an error occurs, saying that
"Row (1) out of range in line 15.
String.append(getResult(“Area”, nResults<)>)"
Does this mean that ) is not necessary? It shouldn’t be…


#4

The issue here is, that getResults is 0-based: try String.append(getResult("Area", nResults-1)); instead.


#5

There’s no more error, but it seems like delete all the previous measurement as well–whenever I measured, the previous measurement is replaced by the new one. I want to keep all the measurements in the Results window.


#6

Can you post the macro that you are executing?


#7

Yeah, I’m following your method so it’s basically what you have got so far:

macro "CopyMeasurement [m]" {
	// Prepare
	run("Clear Results");
	String.resetBuffer; 

	// Measure
	run("Measure");

	// Copy the last measurement to clipboard
	String.append(getResult("Area", nResults-1)); 
	String.copy(String.buffer);
	}

It does exactly the same thing as my original code does, using the new measurement to replace the previous one.


#8

If you want to copy the results directly into excel you could also check out the “Read and Write Excel” plugin available from the update site. I found it very useful for my analysis at least :slight_smile:


#9

Yes. That’s due to run("Clear Results");. Removing that line should keep your previous results around. Sorry, for not realizing that earlier! :frowning:

Best,
Stefan


#10

It works now. Thank you very much!