From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I make a command to calculate the average

Hello,
 
I want to create a command in a script. I have different files with different number of measurements. So in one file I've got for example six variables and in another nine. I want to make a command so it can calculate the average of the variables, but I want to use this formula in a script, I cannot just say (R1+R2+R3....)/9 because of the different numbers of variables. Can anyone help?
0 Kudos
Message 1 of 5
(3,798 Views)
Hello RAJH!
 
I like to help you but I do not understand your exact situation. Can you please tell me what kind of files do you have. Are these measurement data files (*.dat/*.tdm) or something else? You used both terms.
 
Matthias
 
 
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 5
(3,772 Views)

Hello Matthias,

Thank you for replying!

I am working with *TDM files. I am writing a script to automize a couple of calculations, because I have to calculate over a hundred files.The data acquisited are "time", "speed" and "distance". These measurements are recorded from braking tests un dry surface where the driver of the vehicle speeds up to 110 km/h and then starts braking. The data starts recording from 100 km/h till 10 km/h. For this I have written commands to find the distance travelled at 60 km/h and saved these as variables R. I used the formula:

R1=CHD(Find("'Speed 1'<60"), "Distance 1")

The problem is that some tests have 8 repeating measurements and other test have only 5. So in one file I accuire 9 different variables and in the other only 6. I have to calculate the average brakingdistance, but I want to do this with a command to put these averages in a tabel in the report.

Also a problem i have encountered is that I have repeated the formula above for the other Variables (R2-R9), but as i was saying some files only have 5 repeatings and therefore the program gives an error. It doesn't recognize "Speed 7" cause there isn't. Maybe you can help me solve this as well.

Greets,

Robert

 

 

 

 

 

 

0 Kudos
Message 3 of 5
(3,778 Views)
Hallo RAJH!
 
One approach is to use the RV1 real variable vector instead of the Rx variables. Then you can use L1 as a counter.
 
Example:
Dim i

' fill (demo)
L1 = 4
 
RV1(1) = 1.234
RV1(2) = 2.345
RV1(3) = 3.456
RV1(4) = 4.567
 
' calculate sum
R1 = 0
For i=1 to L1
  R1 = R1 + RV1(i)
Next
 
' calculate average
R1 = R1 / L1
 
MsgBox "Average = " & str(R1,"ddd.dd")

Note: The maximum value count for RV1 is 15!

If this doesn't suits you have a look at the DIAdem FormulaCalc command. This command can calculate expressions from a string.

Matthias

Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 4 of 5
(3,764 Views)

OK thank you very much, this helps a lot!

greets,

Robert

0 Kudos
Message 5 of 5
(3,735 Views)