DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a channel by combining the smaller values of two other channels

Solved!
Go to solution

Hello, I have two channels (Volts+ and Volts1+) and I'm trying to create another channel (Volts_Small) that will have the smaller values of the two channels. I need to compare every data point in the two channels and add the smaller value to the new channel. 

 

Ths is what I attempted without success: 

Dim i
Dim Group : Set Group = Data.Root.ChannelGroups("200_68")
Dim Chn1 : Set Chn1 = Group.Channels("Volts+")
Dim Chn2 : Set Chn2 = Group.Channels("Volts1+")
Dim Chn3 : Set Chn3 = Group.Channels("Volts_Small")

For i = 1 to Chn1.Size
  If (Chn1.Values(i) > Chn2.Values(i)) Then
    Chn3.Values(i) = Chn2.Values(i)
  Else
    Chn3.Values(i) = Chn1.Values(i)
  End If
Next

I would very much appreciate help with this! Thanks.

 

Cedric

 

EDIT: 

 

Nevermind, I solved this:

 

Dim i
Dim Group : Set Group = Data.Root.ChannelGroups("200_68")
Dim Chn1 : Set Chn1 = Group.Channels("Volts+")
Dim Chn2 : Set Chn2 = Group.Channels("Volts1+")
Dim Chn3 : Set Chn3 = Group.Channels("Volts_Small")

For i = 1 to 900000
  If (Chn1.Values(i) > Chn2.Values(i)) Then
    Chn3.Values(i) = Chn2.Values(i)
  Else
    Chn3.Values(i) = Chn1.Values(i)
  End If
Next
0 Kudos
Message 1 of 5
(4,420 Views)
Solution
Accepted by topic author CedricE

Hi Cedric,

 

I would strongly advise you to use the Channel Calculator and the MinV() function that is already built into DIAdem.  You have less code that will run much faster, particularly for larger data sets.

 

Dim Group : Set Group = Data.Root.ChannelGroups("200_68")
Dim Chn1 : Set Chn1 = Group.Channels("Volts+")
Dim Chn2 : Set Chn2 = Group.Channels("Volts1+")
Dim Chn3 : Set Chn3 = Group.Channels("Volts_Small")
Dim Variables : Variables = Array("Ch1", "Ch2", "Ch3")
Dim Channels : Channels = Array(Chn1, Chn2, Chn3)
Call Calculate("Ch3 = MinV(Ch1, Ch2)", Variables, Channels)

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 2 of 5
(4,397 Views)

Thanks Brad, I appreciate your help.

I have another issue related to the code in my first post. When I try to do a For statement for i = 1 to Chn1.Size I get this error:

"The index 999789 is outside the valid range for the Values property."

That number is the amount of data points in that channel and I need to run the for statement for the entire channel. Do you have any suggestions?

Oddly, when I substract 1000 from the Chn1.Size, it works but of course it doesn't run the code for those last 1000 data points.

 

Another example of that problematic For statement in my script:

 

For i = 2 to Chn1.Size
  If ((Chn6.Values(i) = 0) And (Chn6.Values(i-1) <> 0)) Then
    NumberPulses = NumberPulses + 1
  End If
Next
0 Kudos
Message 3 of 5
(4,387 Views)

Hi Cedric,

 

Why are you looping over the Size of Chn1, when you're comparing the values in Chn6?  In rare cases the ChnLength() function is more helpful than the Size property.  What happens if you try this?

 

iMax = ChnLength(Chn6)
For i = 2 to iMax
  If (Chn6(i) = 0) And (Chn6(i-1) <> 0) Then
    NumberPulses = NumberPulses + 1
  End If
Next

Again, Ideally you'd use a Channel Calculator expression here, in combination with a new channel that stores the previous row's value in it.  But if your VBScript code runs fast enough for you, I'll stop pestering you about that.


Brad Turpin

DIAdem Product Support Engineer

National instruments

0 Kudos
Message 4 of 5
(4,363 Views)

That worked really well, thanks.

I don't understand the logic behind the Channel Calculator expression and my script is running fast enough for my purposes.

 

Thanks again,

Cedric

0 Kudos
Message 5 of 5
(4,355 Views)