12-11-2020 05:22 AM - edited 12-11-2020 05:38 AM
Hello,
Not for the first time, I'm being driven spare by the Find function producing incorrect results. After having run hundreds of lines of code, I get to a Find function that is looking for the first value greater than zero in a channel holding the X values of a curve fit. The code is:
Dim XloFit
Set XloFit = Data.GetChannel("Working/XloFit")
L1 = Find("Ch(""" & XloFit.Name & """) > 0", 1)
Logfilewrite L1
Logfilewrite "Value at row L1-1 = " & Data.Root.ChannelGroups("Working").Channels(XloFit.Name).Values(L1-1)
Logfilewrite "Value at row L1 = " & Data.Root.ChannelGroups("Working").Channels(XloFit.Name).Values(L1)
Logfilewrite "Value at row 61 = " & Data.Root.ChannelGroups("Working").Channels(XloFit.Name).Values(61)
Logfilewrite "Value at row 62 = " & Data.Root.ChannelGroups("Working").Channels(XloFit.Name).Values(62)
The logfilewrite returns are:
93
Value at row L1-1 = 5.05693077441787E-02
Value at row L1 = 0.050789255059102
Value at row 61 = -5.13476094361203E-04
Value at row 62 = 9.96610097161387E-04
I know that the zero crossing is at row 62 but I cannot configure the Find string to produce anything other than 93.
As a trial, I stripped out all the other groups except "Working", saved the file and re-ran the code. It worked!
I then re-ran the code on the saved full file with all the groups in and it fails. There is only one group called "Working" and I know that the code is looking at the right channel because the values returned are real.
I'm baffled so I'd be grateful for any clues. Incidentally, ChnFind exhibits the same problem.
Regards, Simon.
EDIT: The following lines of code both work:
L1 = Find("Ch(""" & XloFit.ChannelGroup.Name & "/" & XloFit.Name & """) > 0", 1)
L1 = Find("Ch(""Working/XloFit"") > 0", 1)
I've never had to use the construction in the first line above before. This is clearly an inefficient way to code so any clues as to what's going on would be good. Thanks.
Solved! Go to Solution.
12-27-2020 02:50 PM - edited 12-27-2020 02:52 PM
The problem is your channel reference in your Find() command. It is ambiguous because more than one channel in the Data Portal has the name "XloFit". Always include the group reference. The script below will replicate your problem, and demonstrate the solution.
Dim oGrp, oChn, iRow
Call Data.Root.Clear: Call Portal.Refresh
Set oGrp = Data.Root.ChannelGroups.Add("Working1")
Set oChn = oGrp.Channels.Add("XloFit",DataTypeChnFloat64)
Call oChn.SetValues(0.0, 1, 62, eValueBlockValueOverwrite)
Call oChn.SetValues(5.05693077441787E-02, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(0.050789255059102, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(-5.13476094361203E-04, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(9.96610097161387E-04, oChn.Size+1, 1, eValueBlockValueOverwrite)
'The first 62 values (rows) in oChn are 0.0, the 63rd & 64th row values are > 0.0
'Create Simon Aldworth's X channel data..
Set oGrp = Data.Root.ChannelGroups.Add("Working")
Set oChn = oGrp.Channels.Add("XloFit",DataTypeChnFloat64)
Call oChn.SetValues(0.0, 1, 91, eValueBlockValueOverwrite)
Call oChn.SetValues(5.05693077441787E-02, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(0.050789255059102, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(-5.13476094361203E-04, oChn.Size+1, 1, eValueBlockValueOverwrite)
Call oChn.SetValues(9.96610097161387E-04, oChn.Size+1, 1, eValueBlockValueOverwrite)
'The first 91 values (rows) in oChn are 0.0, the 92nd & 93rd row values are > 0.0
'The channel reference in the Find command is ambiguous, causing the first channel with the correct name to be used by Find().
Set oChn = Data.GetChannel("Working/XloFit")
iRow = Find("Ch(""" & oChn.Name & """) > 0", 1)
Call LogFileWrite("Channel '" & oChn.GetReference(eReferenceNameName) & "' first value > 0 is at row " & iRow)
'Below the channel reference passed to the Find() command is properly formatted.
Set oChn = Data.GetChannel("Working/XloFit")
iRow = Find("Ch(""" & oChn.GetReference(eReferenceIndexIndex) & """) > 0", 1)
Call LogFileWrite("Channel '" & oChn.GetReference(eReferenceNameName) & "' first value > 0 is at row " & iRow)
The LogFile output is:
Channel 'Working/XloFit' first value > 0 is at row 63
Channel 'Working/XloFit' first value > 0 is at row 92
01-04-2021 03:34 AM
Hi,
Thanks for your reply. I must admit I remain totally confused by this problem for the following reason. I thought that using the Set statement with GetChannel would explicitly define the required channel within the data portal, irrespective of whether the same channel name was used in other groups. This is borne out by the fact that XloFit.Maximum will find the required value regardless of whether XloFit appears as a channel name in a prior group. How can XloFit.Name be looking at the first channel it finds with the name XloFit when XloFit.Maximum is looking at the channel applied by the Set statement?
Regards, Simon.
01-04-2021 03:55 AM
Hi,
I think the penny has dropped. I didn't realise that using XloFit.Name literally returned the channel name and NOT a reference to it.
I understand the problem now so thanks for your help and I'll mark it as the solution.
Regards, Simon.