12-02-2019 04:56 AM
Hello, I need to use the function "ChnStatisticsChannelCalc" like below:
Set ChnResult = ChnStatisticsChannelCalc("'[2]/MAIN_T_OUT_act','[3]/MAIN_T_OUT_act'",32,15,45,0,1,1,"NameName")
The script is to automatic get all the group channel name in string.
Attached is the code.
for i =0 to n-2
oChname(i) = "[" & str(i+2) &"]" &"/"& str(oChn)
next
Here has a error "subscript out of range".
Also I need the oChstr can contain all group selected channel name in string.
Please help me to check it.
Thanks a lot
Solved! Go to Solution.
12-04-2019 06:01 PM
This script demonstrates how to get all of the channel group and channel names from the file loaded in the Data Portal, and to calculate statistics on each channel:
Call Data.Root.Clear
Call DataFileLoadSel(sFilePathDIAdemExample("Example.tdm"),"TDM","[1]/*")
Dim oGrp, oChn
For Each oGrp In Data.Root.ChannelGroups()
Call LogFileWrite("Channel group '" & oGrp.Name & "' has these channels:")
For Each oChn In oGrp.Channels
Call LogFileWrite(String(1,vbTab) & "channel name: '" & oChn.Name & "'")
'Calculate statistics for channel oChn and add as properties..
Set ChnResult = ChnStatisticsChannelCalc(oChn, eStatsMinimum + eStatsMaximum + eStatsGeometricMean)
Next
Next
Function sFilePathDIAdemExample(ByVal sFilename)
'Returns the full absolute file/path for the location of the DIAdem
'example (TDM/TDMS) file sFilename. Looks in the usual places.
'Returns "" if the file cannot be found.
'v20191119
sFilePathDIAdemExample = ""
Dim arrFilePaths, sFilePath, sFolder: ReDim arrFolders(2): arrFolders(0) = ProgramDrv: arrFolders(1) = CommonDocumentsPath: arrFolders(2) = GetEnv("PUBLIC")
For Each sFolder In arrFolders
arrFilePaths = DirListGet(sFolder,sFilename,"Date/Time","FullFilenamesRecursive")
If IsArray(arrFilePaths) Then
For Each sFilePath In arrFilePaths
If InStr(1,sFilePath,"Libr",vbTextCompare) = 0 Then sFilePathDIAdemExample = sFilePath: Exit For
Next
End If
If Len(sFilePathDIAdemExample) > 0 Then Exit For
Next
If IsArray(arrFilePaths) Then Call Erase(arrFilePaths)
If IsArray(arrFolders) Then Call Erase(arrFolders)
End Function 'sFilePathDIAdemExample()
12-17-2019 02:30 PM
You are re-using "i" in the second loop without re-setting it.
Try adding i = 0 after the first for loop
12-19-2019 05:02 AM
Hi markwkiehl,
This is a good example for get all channel involved.
Also a very each way to get "ChnStatisticsChannelCalc" used.
In my condition, I still need the channel calculation in Row selected, for using in GUI and selected by different request.
12-19-2019 05:33 AM - edited 12-19-2019 05:34 AM
It is not clear to me what you need. Sorry, but I don't understand the statement "In my condition, I still need the channel calculation in Row selected, for using in GUI and selected by different request.".
I get the hint from your initial post that the user somehow selects a channel group. If so, please explain in detail how that channel group is selected. Is the channel group selected in the Data Portal, the Navigator, or the View panel channel table?
You mention in your last post "..Row selected". How is the channel row selected by a user? In the Data Portal, in a View channel table?
If you want to calculate channel statistics by channel row, then you need to specify a range of rows, or assume the first to one selected, or the one selected to the last. How do you intend to get that range from the user? I don't see any evidence in your script of a specific channel row being selected.
Note that command ChnStatisticsChannelCalc() calculates channel statistics channelwise. Command ChnStatisticsBlockCalc() calculates statistics rowwise.
'Reference the last channel group
Dim oGrp
Set oGrp = Data.Root.ChannelGroups(Data.Root.ChannelGroups.Count)
'Reference the 2nd channel in oGrp
Dim oChn
Set oChn = oGrp.Channels(2)
12-19-2019 07:56 PM
Hello markwkiehl ,
This function is in "DIAdem ANALYSIS > Statistics > Descriptive Statistics", and below is the recorded script while using this.
It means calculated the "Arithmetic mean" to channel named from Row 10 to 1000, which will enable me to calculate the channel value from a selected beginning row to a end row.
Call ChnStatisticsChannelCalc("[1]/Drehzahl",32,10,1000,0,0,0,"NameName")
And this is my first time to see ChnStatisticsBlockCalc.
I think I do not know how to use it for the description in DIAdem for ChnStatisticsChannelCalc and ChnStatisticsBlockCalc looks the same.
12-22-2019 07:06 PM
I have finished this script for enable the row select function, see attached.
The using condition is many groups the same channels to get the average.
It will create two new group for channel name and value.
While when I want to put all values to one channel , alarm is " This channel was created with the type DataTypeFloat64. The filling values that you use must not be type vbObject."
12-23-2019 01:48 PM - edited 12-23-2019 01:51 PM
Your function takes arguments to calculate the arithmetic mean, but then within the function it attempts to calculate the arithmetic mean for all channels within a channel group. I suggest you study my Data Portal script example at: http://www.savvydiademsolutions.com/dataportal.php?topic=data-portal-basics
Meanwhile, I have provided you two functions. One that calculates the for one channel by the specified row range, and another for all channels within a specified channel group. I also present a more direct way to calculate the arithmetic mean for one or more channels. Note my first example where I define "Dim oElementList: Set oElementList = Data.CreateElementList" and then pass oElementList to ChnStatisticsChannelCalc(). For most DIAdem functions & commands, you may pass either a channel list variable (oElementList), or a channel object variable (oChn). These examples should clarify several issues.
Option Explicit
Call LogFileDel
'Load a DIAdem example data file
Call Data.Root.Clear
Call DataFileLoadSel(sFilePathDIAdemExample("Example.tdm"),,"[1]/*")
'Highlight the commented rows below and use 'Shift-Ctrl-D' to uncomment the lines.
'Then run the script to observe the function running.
'Highlight the rows below again and use 'Ctrl-D' to re-comment the lines.
''Calculate the arithmetic mean for all channels in the last channel group (excluding the first channe "Time")
'Dim oElementList: Set oElementList = Data.CreateElementList
'Dim i
'For i = 2 To Data.Root.ChannelGroups(1).Channels.Count
' Call oElementList.Add(Data.Root.ChannelGroups(1).Channels(i))
'Next
'Call ChnStatisticsChannelCalc(oElementList,eStatsArithmeticMean,1,Data.Root.ChannelGroups(1).Channels(1).Size,False,False)
''Calculate the arithmetic mean for the the 2nd channel in the first channel group
'Call LogFileWrite("dChnMeanByRows() = " & dChnMeanByRows(1, 2, 1, Data.Root.ChannelGroups(1).Channels(2).Size))
'Calculate the arithmetric mean for all of the channels in the first channel group
If Not bGrpChnMeanByRows(1, 1, Data.Root.ChannelGroups(1).Channels(2).Size) Then Call Err.Raise(65535,,"ERROR - invalid parameters or channels passed to bGrpChnMeanByRows()")
Function dChnMeanByRows(iGrpNum, iChnNum, iRowStart, iRowEnd)
dChnMeanByRows = NoValue
If Data.Root.ChannelGroups.Count = 0 Then Exit Function
'Validate the function arguments iGrpNum, iChnNum, iRowStart, iRowEnd
If IsEmpty(iGrpNum) OR iGrpNum < 1 Then Call Err.Raise(65535,,"ERROR - invalid function argument iGrpNum = '" & iGrpNum & "'")
If IsEmpty(iChnNum) OR iChnNum < 1 Then Call Err.Raise(65535,,"ERROR - invalid function argument iChnNum = '" & iChnNum & "'")
If IsEmpty(iRowStart) OR iRowStart < 1 Then iRowStart = 1
If IsEmpty(iRowEnd) OR iRowEnd < 1 Then iRowEnd = Data.Root.ChannelGroups(iGrpNum).Channels(iChnNum).Size
Dim oGrp, oChn
Set oGrp = Data.Root.ChannelGroups(iGrpNum)
Set oChn = oGrp.Channels(iChnNum)
If oChn.DataType = DataTypeChnFloat64 AND oChn.IsKindOf(eDataChannel) Then
Call ChnStatisticsChannelCalc(oChn, eStatsArithmeticMean, iRowStart, iRowEnd, False, False)
'The arithmetic mean has been added to oChn as a property named: "Result~Statistics~MeanValues~ArithmeticMean"
dChnMeanByRows = oChn.Properties("Result~Statistics~MeanValues~ArithmeticMean").Value
End If
Set oChn = Nothing: Set oGrp = Nothing
End Function 'dChnMeanByRows()
Function bGrpChnMeanByRows(iGrpNum, iRowStart, iRowEnd)
bGrpChnMeanByRows = False
If Data.Root.ChannelGroups.Count = 0 Then Exit Function
'Validate the function arguments iGrpNum, iChnNum, iRowStart, iRowEnd
If IsEmpty(iGrpNum) OR iGrpNum < 1 Then Call Err.Raise(65535,,"ERROR - invalid function argument iGrpNum = '" & iGrpNum & "'")
If IsEmpty(iRowStart) OR iRowStart < 1 Then iRowStart = 1
If IsEmpty(iRowEnd) OR iRowEnd < 1 Then iRowEnd = Data.Root.ChannelGroups(iGrpNum).Channels(iChnNum).Size
Dim oGrp, oChn, iChn
Set oGrp = Data.Root.ChannelGroups(iGrpNum)
For iChn = 1 To oGrp.Channels.Count
Set oChn = oGrp.Channels(iChn)
If oChn.DataType = DataTypeChnFloat64 AND oChn.IsKindOf(eDataChannel) Then
Call ChnStatisticsChannelCalc(oChn, eStatsArithmeticMean, iRowStart, iRowEnd, False, False)
'The arithmetic mean has been added to oChn as a property named: "Result~Statistics~MeanValues~ArithmeticMean"
End If
Next
Set oChn = Nothing: Set oGrp = Nothing
bGrpChnMeanByRows = True
End Function 'bGrpChnMeanByRows()
Function sFilePathDIAdemExample(ByVal sFilename)
'Returns the full absolute file/path for the location of the DIAdem
'example (TDM/TDMS) file sFilename. Looks in the usual places.
'Returns "" if the file cannot be found.
'v20191119
sFilePathDIAdemExample = ""
Dim arrFilePaths, sFilePath, sFolder: ReDim arrFolders(2): arrFolders(0) = ProgramDrv: arrFolders(1) = CommonDocumentsPath: arrFolders(2) = GetEnv("PUBLIC")
For Each sFolder In arrFolders
arrFilePaths = DirListGet(sFolder,sFilename,"Date/Time","FullFilenamesRecursive")
If IsArray(arrFilePaths) Then
For Each sFilePath In arrFilePaths
If InStr(1,sFilePath,"Libr",vbTextCompare) = 0 Then sFilePathDIAdemExample = sFilePath: Exit For
Next
End If
If Len(sFilePathDIAdemExample) > 0 Then Exit For
Next
If IsArray(arrFilePaths) Then Call Erase(arrFilePaths)
If IsArray(arrFolders) Then Call Erase(arrFolders)
End Function 'sFilePathDIAdemExample()