12-08-2023 03:50 AM
Hello,
I'm trying to merge two element lists coming from two different groups. Some how the merge is not properly done. Is there a way to do it?
I can create a list of element coming from same group, but not coming from two different group.
Thank you for your help
12-08-2023 07:21 AM
Hello ThierryAlstom,
Are you referring to data channel lists that you want to merge in order to continue working with them?
You can obtain a channel list of specific channels using the Data.GetChannels(Reference) function. Use the Reference parameter to specify whether you want channels from a specific group or channels from several groups. In the case of multiple groups, no group name is specified, only the channel name or parts of the channel name, e.g. *Noise* for all channels that contain Noise in the name.
If you want to merge two channel element lists, then use the function AddElementList of the element list object. Take a look at the help page to learn more about the second parameter, which specifies how the lists are merged.
I've prepared an example for you:
' Load data only to show the example.
Call Data.Root.Clear()
Call DataFileLoad("EXAMPLE.TDM", "", "Load|ChnXYRelation")
' first possibility: get all channels over part of its name
dim Channels
set Channels = Data.GetChannels("*Noise*")
print("channel count: " & Channels.Count)
' second possibility: use AddElementList
dim ChnListMerge, ChnListNoise
set ChnListMerge = Data.GetChannels("Example/[1]") ' First channel of group Example
set ChnListNoise = Data.GetChannels("Noise data/*") ' All channels of group Noise data
call ChnListMerge.AddElementList(ChnListNoise, ListMergeModeAdd)
' only to show the content of the merged list
dim Chn
for each Chn in ChnListMerge
print(Chn.GetReference())
next
12-08-2023 07:39 AM
Thanks for your reply.
So the merge works. But the issue is when I tried to save the channelist in CSV file. If I keep your code as example, it will save only the data from group "example", using this command line:
Call DataFileSaveSel(path+"TestResult.csv", "CSV", ChnListMerge)
12-08-2023 08:57 AM
Unfortunately, the DataFileSaveSel function cannot save multiple groups if you use CSV. This is pointed out in the help.
If you do not want to save the channel list as a TDM file, but still as a CSV file, there is the following workaround, which is not very elegant, but works. Copy all channels of the element list into a temporary group and specify this when saving. Then delete the group.
I have adapted the example as follows:
' Load data only to show the example.
Call Data.Root.Clear()
Call DataFileLoad("EXAMPLE.TDM", "", "Load|ChnXYRelation")
dim ChnListMerge, ChnListNoise
set ChnListMerge = Data.GetChannels("Example/[1]")
set ChnListNoise = Data.GetChannels("Noise data/*")
call ChnListMerge.AddElementList(ChnListNoise, ListMergeModeAdd)
' Create a temporary group and copy the channels into it to save them afterwards.
dim Chn, TempGroup
set TempGroup = Data.Root.ChannelGroups.Add("TempGroupForSaving")
for each Chn in ChnListMerge
call TempGroup.Channels.AddChannel(Chn)
next
call DataFileSaveSel(CurrentScriptPath & "TestResult.csv", "CSV", TempGroup)
' Delete temporary group
call Data.Root.ChannelGroups.Remove(TempGroup.Name)
12-08-2023 09:43 AM
Yes I saw that but I was hoping that I missed something...
Thanks.
12-11-2023 01:12 AM
Unfortunately, the CSV export can only export one group. Copying the desired channels into one group is therefore the only workaround. Does the described solution work for you?
12-11-2023 02:35 AM
I understood. That works for me. thank you