DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Delete a channel without knowing which group it is in

Solved!
Go to solution

Hi,

 

I regularly use a few lines of code that calculate the average of some points of data in the following way:

 

Set ChnResult = ChnStatisticsChannelCalc(YRChn, 32, L2, L3, , True)
YRss = ChnResult.Item(1).Values(1)
Call Data.Root.ChannelGroups(1).Channels.Remove("ArithmeticMean")

 

But I am now finding I need to use the code when there are multiple groups in the data file and I might not know which group is the default. This poses a couple of questions:

1. Can I control where the result of ChnStatisticsChannelCalc is placed?

2. Can I delete a channel by knowing only its name and not which group it's in?

 

The answer to the first question might make the second question obsolete but it would still be good information. The answer to the second question would always be useful to know. Effectively, I would like to put an * in the ChannelGroups index.

 

Thanks, Simon.

0 Kudos
Message 1 of 6
(3,139 Views)
Solution
Accepted by topic author Simon_Aldworth

Following code would help you

 

Option Explicit
' load some data
data.Root.Clear
DataFileLoad ProgramDrv & "Libr\Data\Example.tdm", "TDM"
dim YRChn : set YRChn = Data.Root.ChannelGroups(1).Channels("Geschwindigkeit")

' new channels will be generated in the active group
data.Root.ChannelGroups(1).Activate

' calculate statistics
Set ChnResult = ChnStatisticsChannelCalc(YRChn, 32, 1, 10, , True)
' extract content
dim YRss : YRss = ChnResult.Item(1).Values(1)

' delete the channels
dim tempChannel : for each tempChannel in ChnResult
  dim grpO : set grpO = tempChannel.ChannelGroup
  grpO.Channels.Remove(tempChannel.name)
Next

 

 

 

 

 

0 Kudos
Message 2 of 6
(3,121 Views)

Thanks Andreas.

 

For the moment I have gone with:

Set ChnsToDel = Data.GetChannels("*ArithmeticMean*")
Call Data.Remove(ChnsToDel)

...because I'm only calculating one statistic at a time.

 

I will mark your reponse as the solution though.

 

I assume it isn't possible to directly control where the result of ChnStatisticsChannelCalc is placed within the function call itself. The only way being to set the default group prior to the calculation?

 

It would be an even better function if you had the option to just take the answer and not create the channel. I rarely need the channel so it's almost always deleted in the following line of code!

 

Regards.

0 Kudos
Message 3 of 6
(3,106 Views)
Solution
Accepted by topic author Simon_Aldworth

A couple of items:

  • The default group is accessible via Data.Root.ActiveChannelGroup
  • To clarify Simon's code, it's safer to do only do the deletion if a channel (or set of channels) exists:
    • Set oChnsToDel = Data.GetChannels("*/ArithmeticMean*")
    • If oChnsToDel.Count > 0 then Call Data.Remove(oChnsToDel)
  • Per the DIAdem help, ChnStatisticsChannelCalc does not return a channel by default.  The last parameter in your call is being set to "TRUE" which is forcing it to export a channel.  Rather than exporting an output, you can access the statistic output via "StatsResult(#)" without exporting a channel.  For example, StatsResult(eStatsArithmeticMean) would be StatsResult(32).
    • Note that I believe StatsResult may only be available in DIAdem 2019 SP1 and beyond (I think it may not have been present in base 2019). 
    • To reference Andreas' example:
      • Option Explicit
        ' load some data
        data.Root.Clear
        DataFileLoad ProgramDrv & "Libr\Data\Example.tdm", "TDM"
        dim YRChn : set YRChn = Data.Root.ChannelGroups(1).Channels("Geschwindigkeit")

        ' calculate statistics
        Set ChnResult = ChnStatisticsChannelCalc(YRChn, eStatsArithmeticMean, 1, 10)  'For clarity, using eStatsArithmeticMean rather than "32" and leaving off the "True"
        ' extract content
        dim YRss : YRss = StatsResult(eStatsArithmeticMean)  'Reference the stat result directly

0 Kudos
Message 4 of 6
(3,050 Views)

Thank you for this update. In particular because I tried to get my code to work without generating the result channel and couldn't. I don't need the channel in 99% of cases so this is a welcome improvement. I can't work out why my original attempt at it didn't work; that's lost in time.

 

Regards.

0 Kudos
Message 5 of 6
(3,038 Views)

Hi,

 

I've come across a need to check whether a channel exists before setting an object variable to represent it. I think the code you offer above will fail because if the channel "*/ArithmeticMean*" does not exist then the Set statement will cause an error.

 

I will post separately to ask for neat ways to make the check first, before using the Set statement.

 

Regards, Simon.

0 Kudos
Message 6 of 6
(3,008 Views)