10-07-2015 05:07 PM - edited 10-07-2015 05:23 PM
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
Solved! Go to Solution.
10-08-2015 10:57 AM
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
10-08-2015 03:09 PM
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
10-09-2015 01:36 PM
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
10-09-2015 05:19 PM
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