cancel
Showing results for
Did you mean:

Solved!
Go to solution

## To put it in a FOR loop

Dear anyone,

I have written a code which calculates averages for 26 different groups. Attached the sample code in .txt file for reference. I would like to put it in a FOR loop to avoid writing a lenghthier code to calculate averages for 26 different groups. Attached code contains average calculation for 2 groups.

Request you to help for the same.

Regards,

X. Ignatius

Message 1 of 13
(5,172 Views)

## Re: To put it in a FOR loop

Hi,

I'm very confused with your code. Do you want to have averages for 26 different groups in one group or in 26 groups? You don't have to copy values for new channel and delete the channels then. Simply use chd to get values from channels.

Simyfren

Message 2 of 13
(5,167 Views)

## Re: To put it in a FOR loop

Dear simy,

I would take a specific block of the 2nd channel of each group, compute the average and store it in the channel name Torque_avg in the same group.

Have to repeat this for each 26 group and store the averages in their respective groups. Hope you understood now.

I appologize if I am not clear in the first post.

Thanks and Regards,
X. Ignatius

Message 3 of 13
(5,164 Views)

## Re: To put it in a FOR loop

oh,

I see now.

Try something like this:

'__________________________________________________

for i = 1 to 26
Call ChnAlloc("Torque" & i, length(i)+1, 1, DataTypeFloat64, "Numeric",2)

Call ChnAlloc("Torque"&i&"_1",length(i)+1, 1, DataTypeFloat64, "Numeric",2)

Call ChnAlloc("Torque_Avg",3, 1, DataTypeFloat64, "Numeric",2)

Call datablcopy("[2]/[2]",Time_interval(i),length(i)+1,"Torque1")

Call chnsum("Torque1","Torque1_1")

LastRow = cch("Torque1_1",2)

Avg1 = LastRow/(length(i)+1)

Avg1 = Avg1 - Tare_avg

chd(1,"Torque_Avg") = Avg1

chd(1,"T1") = Avg1
'
'
'
'rest of average calculation
'
'
next

'__________________________________________________

where length() and Time_interval() are arrays (in the case when this values are different for each group).

Hope this helps!

Simyfren

Message 4 of 13
(5,159 Views)

## Re: To put it in a FOR loop

Dear Simy,

Thanks for the input.

Still I have problem with the data block copy command. How to reference the group when I use it inside the FOR loop.

I cannot have unique channel names, as it would changes from test to test.

I cannot have unique group names, as it changes from test to test.

Could you help in this regard.

Regards,

X. Ignatius

Message 5 of 13
(5,148 Views)

## Re: To put it in a FOR loop

Hi,

group referencing should look this ways:

'______________________________________

"[i]/channelname" 'if the channel name is the same

"[i]/[j]" 'if the channel order is the same

'______________________________________

Hope this helps, if not, give me a concrete example what do you want.

Simyfren

Message 6 of 13
(5,145 Views)

## Re: To put it in a FOR loop

Hi Simy,

This is what I tried but it is not working. What could be the reason for it?

Regards,

X. Ignatius

Message 7 of 13
(5,141 Views)

## Re: To put it in a FOR loop

And what is wrong?

Did you get some error?

Simyfren

Message 8 of 13
(5,139 Views)

## Re: To put it in a FOR loop

Simy,

I couldn't find out what was wrong. Attached the error screen for reference.

Regards,

X. Ignatius

Message 9 of 13
(5,137 Views)

## Re: To put it in a FOR loop

Simy,

I couldn't find out what was wrong. Attached the error screen for reference.

Regards,

X. Ignatius

Message 10 of 13
(5,137 Views)