DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a new channel and adding values to it based on values in other channel

Solved!
Go to solution

Hello,

I am fairly new to DIAdem and I am trying to do basic channel operation.

I have a channel where the values are text (True or False). I want to create a new channel and add values to the new channel based on the values in the first channel. If the first channel value is true, I want to add 1 and if the value it is False, I want to add 0. I have written a script where it creates a new channel. And I am trying to use if...then...else statement to set my conditions for each true or false values statement. I am stuck on how to cross reference the first channel within the if statement so that I can set my 'then' and 'else' condition.

I am reaching out to experts here to get this sorted out. Kindly let me know what I should be doing here. Thank you.

0 Kudos
Message 1 of 3
(2,042 Views)

Hi Ram,

 

Here's the best performing solution to your scenario-- using the Calculate() command.

' Create a TRUE / FALSE text channel like you describe
DataFilePath = "C:\PROGRAM FILES\NATIONAL INSTRUMENTS\DIADEM 2021\Examples\Data\Weather_Info.tdm"
Call Data.Root.Clear()
Call DataFileLoadSel(DataFilePath, "TDM", "[1]/[2]")
Set Group = Data.Root.ChannelGroups(1)
Set TxtChannel = Group.Channels("Season")
Formula = "TxtChn = IIF(TxtChn = ""Fall"", ""False"", ""True"")"
Call Calculate(Formula, Array("TxtChn"), Array(TxtChannel))
ChnLength(TxtChannel) = 26

' Create a numerical channel that has corresponding 1 / 0 values
Set NumChannel = Group.Channels.Add("Number", DataTypeChnFloat64)
Formula = "NumChn = IIF(UCase(TxtChn) = ""TRUE"", 1, 0)"
Call Calculate(Formula, Array("TxtChn", "NumChn"), Array(TxtChannel, NumChannel))

Here's what you're directly requesting.

' Create a TRUE / FALSE text channel like you describe
DataFilePath = "C:\PROGRAM FILES\NATIONAL INSTRUMENTS\DIADEM 2021\Examples\Data\Weather_Info.tdm"
Call Data.Root.Clear()
Call DataFileLoadSel(DataFilePath, "TDM", "[1]/[2]")
Set Group = Data.Root.ChannelGroups(1)
Set TxtChannel = Group.Channels("Season")
Formula = "TxtChn = IIF(TxtChn = ""Fall"", ""False"", ""True"")"
Call Calculate(Formula, Array("TxtChn"), Array(TxtChannel))
ChnLength(TxtChannel) = 26

' Create a numerical channel that has corresponding 1 / 0 values
Set NumChannel = Group.Channels.Add("Number", DataTypeChnFloat64)
iMax = TxtChannel.Size
FOR i = 1 TO iMax
  IF UCase(TxtChannel(i)) = "TRUE" THEN
    NumChannel(i) = 1
  ELSE
    NumChannel(i) = 0
  END IF
NEXT ' i

Brad Turpin

Principal Technical Support Engineer

NI

0 Kudos
Message 2 of 3
(1,992 Views)
Solution
Accepted by topic author Ram_Krish

Hi Brad,

Thank you very much for the response. I don't want to create a text channel. I have already collected the data through LabVIEW. The collected value is of True or False. I have written the following script to do the conversion. This is a very crude way of how I put together from what I learnt. 

 

Dim i
For i = 1 to Groupcount
Set Group = Data.Root.ChannelGroups(i)
Call GroupDefaultSet(i)
Set TxtChannel = Group.Channels("X")
Set NumChannel = Group.Channels.Add("X_numeric", DataTypeChnFloat64)
Formula = "NumChn = IIF(UCase(TxtChn) = ""TRUE"", 1, 0)"
Call Calculate(Formula, Array("TxtChn", "NumChn"), Array(TxtChannel, NumChannel))
Next

 

This script works perfectly, but please do correct me if I am wrong somewhere. Thank you.

 

Regards,

Ram.

0 Kudos
Message 3 of 3
(1,972 Views)