DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Coverting Text Channel to Numberic using Dict.Add function

Solved!
Go to solution

I have multiple channels that have a text value to them and I was using a Dict.add function to change the values to a Number so it can be plotted linearly.  However I have had to create a seperate function for each channel in the data set.  I want to generalize it more so it will go out and look a all the channels and compare the values to the dictionary's already created.  I have attached a sample form of how I currently am doing the change and I have played with a new form called TxtToNum.vbs that is more generalized.  I have 6 dictionary's that I am using in total as well if that plays into the task.   I don't do a lot of script work and could really use some help.

 

Thanks - J

 

 

0 Kudos
Message 1 of 15
(7,189 Views)

I continue to try and play with this but can't seem to get it.  I am missing the logic somewhere.  I am trying to take a text value and compare it to my dictionary and then set the number in the text value's place

 

Like "On","1"  So my channels would end up being a numeric channel and my "On" would turn into a "1" according to my dict3.


UGGGG!

0 Kudos
Message 2 of 15
(7,174 Views)

Ok, got it to work but now I have a TEXT channel still and need to convert it to numeric values.  How can I add that to this script?

0 Kudos
Message 3 of 15
(7,163 Views)

Hi Jcheese,

 

Unfortunately there isn’t a direct way to convert a string/text channel to a numeric channel that I can find.

 

If you’re just pulling out individual values,

CStr() converts a numeric to a string. CInt() or CDbl() will convert a string to an Integer or a Double.

 

If you need to convert the whole channel, you would probably need to create a second channel, initialized to the same length as the Text channel, and then loop through the channel. Pull the text value out of the existing Text channel, use one of the functions above to convert it to a number, then set the value at the same index in the new channel.

Here’s a reference for creating a channel and setting the values:

http://zone.ni.com/reference/en-XX/help/370859J-01/procauto/procauto/procauto_group_channel_generate...

 

Kelsey Johnson

Applications Engineering

National Instruments

0 Kudos
Message 4 of 15
(7,148 Views)

Hello JCheese,

 

please try this :

 

Dim   sgCmd
sgCmd = "ch(""#1"") = Val(ch(""#1""))"
sgCmd = Replace(sgCmd,"#1","MyChannelname")
Calculate(sgCmd)

 

The text MyChannelname needs to be replace with the name of the channel you want to convert

This uses the DIAdem "VAL" operator on the whole channel

 

Message 5 of 15
(7,136 Views)

Hello and thanks for the reply,

 

I tried the script you copied and it changing the channel to a numeric channel however it turns all my values into "NOVALUES"

 

My attachment that I have on this post is a working script that I am running and I wish to add the conversion factor into that script so as it converts it to the dictionary key it changes the channel to a numeric channel.  Does anyone know how I could write that to work?  I am not that familiar with script to write this kind of file.  I have added my old file that I used which did make a seperate channel and put in the values like I want but it takes a long time to run.  I am going through sometimes thousands of rows of data.

 

 

0 Kudos
Message 6 of 15
(7,123 Views)

Hello JCheese,

running your script I saw that for those channels where you got NoValues, your mapping from text to the desired Integer didn't work. So after your "ConvertTxtToNum" you still have text in the channel which is like "Reading Not Available" in channel 49. It looks like your mapping is incomplete. Val("Reading Not Available") returns NoValue. Do you plan to add teh missing text/Value pairs in your map or do you want those strings to be mapped to a standard value ?

 

0 Kudos
Message 7 of 15
(7,118 Views)

JCheese,

 

to better explain whats going on:

If you would change your function to this :

 

Function ConvertTxtToNum(Channel)

  Call Channel.ChannelGroup.Activate
  for t = 1 to Channel.Size
  IF Dict3.Exists(ChT(t,Channel.Name)) THEN               ' Compares value in channel to value in dictionary
  ChT(t,Channel.Name) = Dict3.Item(ChT(t,Channel.Name))   ' changes the Key to the Item in the dictionary
  Else
    ChT(t,Channel.Name) = "0"
  end if  'dict3
  next 
  
End Function ' ConvertTxtToNum()

 You will find that the functions using "Val" works as expected adn doesn't create NoValues anymore

 

0 Kudos
Message 8 of 15
(7,116 Views)

If a value is not present in my dictionary, then I would like it to be set to 0. 

0 Kudos
Message 9 of 15
(7,115 Views)
Solution
Accepted by topic author Jcheese

The following should do what you want:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Text to Numeric Channel
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if programrevision > 1129 then
  Dim Channel,State,Dict3,t,TXT1
    IF Data.Root.ChannelGroups(1).Channels.Count > 0 THEN
      FOR Each Channel In Data.Root.ChannelGroups(1).Channels
  
        IF Trim(UCase(Channel.Properties("DisplayType").Value)) = "TEXT" THEN
        Call ConvertTxtToNum(Channel)
        Call ValChn(Channel)
        END IF
        
      NEXT ' Channel
    END IF ' Any Channels at all 
END IF ' Program Version

Function ConvertTxtToNum(Channel)

  Call Channel.ChannelGroup.Activate
  for t = 1 to Channel.Size
  IF Dict3.Exists(ChT(t,Channel.Name)) THEN               ' Compares value in channel to value in dictionary
  ChT(t,Channel.Name) = Dict3.Item(ChT(t,Channel.Name))   ' changes the Key to the Item in the dictionary
  Else
    ChT(t,Channel.Name) = "0"
  end if  'dict3
  next 
  
End Function ' ConvertTxtToNum()

Function  ValChn(oChannel)
  Dim   sgCmd 
  Dim   ChnNumber : ChnNumber = oChannel.Properties("number").Value
  sgCmd = "ch(""#1"") = Val(ch(""#1""))"
  sgCmd = Replace(sgCmd,"#1",oChannel.GetReference(eRefTypeIndexName))
  Calculate(sgCmd)
End Function

 

Message 10 of 15
(7,112 Views)