Search for Specific Values in Data Channel Using DIAdem Script

Updated Nov 17, 2020

I want to use scripting in DIAdem to find where specific values or a range of values occur in my data set. How can I get started?

Using the FIND function in a DIAdem Script is the quickest way to find specific values in a DIAdem DATA channel.

The basic FIND function’s syntax is:

L3 = FIND("Ch(2) > 1")

In the above example, the internal variable L3 will contain the index (1-indexed) of the first data point that meets the condition 'Ch(2) > 1'. If it does not find anything that meets this condition the FIND function returns a 0.

The FIND function also has additional parameters for more complex searches. For example:

L3 = FIND( "abs(Ch(2)-10)<0.05" , 100 )

In the above example, the internal variable L3 will contain the index (1-indexed) of the first point in channel 2 that is greater than 10. In addition the search for this value is performed with a tolerance of 0.05. Using a tolerance is especially useful when trying to find real values. Also, the search will start looking at data point 100 in channel 2.

Being able to set where the search starts from enables you to search for several of the same values in a channel. For example:

DIM LV1(100)   'Array for holding the indices of the found values (size 100) 
B1 = TRUE      'Initialize loop condition
L1 = 1         'Initialize 
L2 = 1         'Search Start Position
Do While B1 
LV1(L1) = FIND("(Ch(1) > -0.05) and (Ch(1) < 0.05)" , L2)
L2 = LV1(L1)+1 'Re-Initialize pointer 
If LV1(L1)= 0 then 
L1 = L1 +1     'count number of matches
End If 

The above example searches for multiple values that meet the criterion, "(Ch(1) > -0.05) and (Ch(1) < 0.05)". Notice also how the search position is managed, L2 contains the position where each search should start. The internal vector variable LV1(1..15) then contains the different point numbers where the condition was true in Ch(1).

The return value of the FIND function is a integer number that points to the index of the located value. For example, using the following code:

L3 = FIND("Ch(2) > 1")

After execution L3 = 5, this means the 5th value of channel 2 met the criterion "Ch(2) >1". 

The FIND function is often times used to locate the times where specific events in data occur. 

For example, if one of your data channels is a digital signal that represents the start of a test event, and the other channel is time, we can FIND the time that digital event goes high. Using the data below:

Point #, Time, Digital Signal
1, 0.000, 0
2, 0.001, 0
3, 0.002, 5
4, 0.003, 5
5, 0.004, 0

First we want to find the first value of the "Digital Signal" that goes high.

L2 = FIND("Ch("Digital Signal") > 4")

L2 will contain the value 3, representing the 3rd data point in the channel.

So now we want to find what time this signal actually goes high, to do this we can use the DIAdem Script function call CHNVAL(x,y).

R1 = CHNVAL(L2, "Time")

In this case R1 will equal 0.002.

This basic example illustrates how specific events can be queried from a data channel. In the DIAdem Help system search under FIND and CHNVAL for more examples and details.