Write looping statement (do while)

Hi,

I have a column on result table. I would like to sum this column. I have thought to use “do and while” to make it. But I don’t how make it ? How can I write to obtain the sum of column ? (this is the third column naming “volume”)

Thank you

Sum=0
i=1
Do{ somme=somme+i
i=i+1}
while (i<=i+1)

Good day Tom,

please study the relevant docs:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html
http://wiki.cmci.info/documents/ijcourses#macro_programming_in_imagej

After having studied these documents I’m sure you will succeed in solving most of the problems you’ve posted recently. If not, feel free to post again.

Sum == somme?

Regards

Herbie

Hi,

I ve posted it because It doesnt work and I Dont know, how can make it. I ve used the function "getresult(“volume”, i).

Yes I would like to sum all values of the column (“volume”)

Thank you

hoping your help,

Tom,

here you find how to access data in a results table:
https://imagej.nih.gov/ij/developer/macro/functions.html
Here you find how to code a “do”-loop and please respect the syntax:
https://imagej.nih.gov/ij/developer/macro/macros.html

Do ≠ do
Sum ≠ somme

Etc.

Regards

Herbie

1 Like

Herbie,

I think I am going to succeed

function sumColumnTab (Volume) {
	sum=0;
	if (columnLabelList (Volume) >=0) {
		for (i=0; i<nResults(); i++) {
			sum=sum+getResult(volume,i);
		}
	Print(sum);

But At the end the sum doesn t print Could you tell me why ?

Regards,

Tom

As most programming languages, the IJ1 macro language is case sensitive. Please pay attention to using commands and functions exactly as written in the documentation or the function reference, and use the macro recorder whenever possible to get the required commands.

3 Likes

Hi,

I have written this macro and it doesn t work but i have written excatly, it s written in documentation macro.
I have just added the setresult() so when I remove the part “return K” and “function sumColumnTab (kind) {” , I show the addition on table and it works. But I don t want to do “return”

Thank you for you help,

for(i=0; i<nResults; i++) {
	d = getResult("d  [um]", i);
	setResult("kind", i, PI/6*0.000000001*pow(d,3));
}
function sumColumnTab (kind) {
K=0;
for(i=0; i<nResults; i++) {
	K = K + getResult("kind", i);
	setResult("P", i, K);
}
return K;
}

Tom,

there are still some errors.

From where in your macro and how do you call the function?

sumColumnTab( kind ) { etc. }

“kind” appears to be a string. If this is true, you have to remove the inverted commas in getResult(), like

getResult( kind, i );

Fighting against a machine in most cases means that you will lose.

regards

Herbie

Yes I know. Fighting again machine, I ll lose.

It s not a string but “double” as 0.6000 or 0.0001. So the function “return” doesn t work ???

Tom,

actually I don’t understand what you are doing and BTW you didn’t answer my first question.

If variable kind is a number then there should be a column of the results table named after this number. Is this really so?

getResult( "kind", i );

Clueless

Herbie

@tom51

Just popping in on this thread… as you’ve had a lot of posts recently on coding. Would be worth your time to take a pause and read through the links that people have posted in all of your threads… With just a bit of reading, etc - you will find that you can answer a great number of your own questions. Coding is all about testing as well - use print() statements to help you find out what’s what in your own code.

To start on some beginning coding stuff in ImageJ/Fiji - here are some helpful links:

Hope this helps!

eta

image

I want to sum each value of kind.

Clueless,

Thomas

Well Thomas,

here is a short macro (actually two) that should do what you want:

// This part generates an example table with 25 values in column "kind"
for ( i = 0; i < 25; i++ ) {
	setResult( "kind", i, random );
}
// This part sums all values in column "kind" and prints the result to the log-window
sum = 0;
for ( i = 0; i < nResults; i++ ) {
	sum = sum + getResult( "kind", i );
}
print( sum );
exit();
// End

HTH

Herbie

Hi,

Thank you, It works. I don’t what was the problem because I tested the function print this morning and it didnt work. But thank you.

Thomas

And I search a function to sort in order my first column “Area”. I think to use array.sort(array). Do you think it ll be possible ?
Thank you,

Thomas

Thomas,

yes it is of course possible but you have to learn how to write the values from the table to an array and, after sorting, back to the table. You’ve got the docs!

Good luck

Herbie

Last gratis effort for you, Thomas:

// This part generates an example table with two columns 
for ( i = 0; i < 25; i++ ) {
	setResult( "Area", i, random );
	setResult( "kind", i, random );
}
// This part sorts all values in column "Area"
a = newArray( nResults );
for ( i = 0; i < nResults; i++ ) {
	a[i] = getResult( "Area", i );
}
Array.sort( a );
for ( i = 0; i < nResults; i++ ) {
	setResult( "Area", i, a[i] );
}
// This part sums all values in column "kind" and prints the result to the log-window
sum = 0;
for ( i = 0; i < nResults; i++ ) {
	sum = sum + getResult( "kind", i );
}
print( sum );
exit();
// End

Now please learn how to code ImageJ macros which is really easy.

Have success

Herbie

3 Likes

Thank you very much

I haven t done many looping statement so sometime I forget some elements and it doesn t work

Thanks

Tom