From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

How to access the channel values?

Solved!
Go to solution

I am new to DIAdem scripting language.  I have TDMS has one group and several channels with type all String.   How do I read the first 4 characters of each channel values and store it in new channel?  Sample data file attached.

 

I tried for statement in the Script Window

 

Ch("[1]/Result") = Ch("[1]/Frame") 

 

or 

 

Ch("[1]/Result") = Right(Ch("[1]/Frame", 3)

 

All gave me error ...

0 Kudos
Message 1 of 8
(6,914 Views)

Hi NiCoder,

 

You were on the right track, but you need to call that sort of expression using the ChnCalculate() command, which will take a reference to the whole channel array of values and apply the formula cell by cell through all the values in the channel array.  I'm including 3 version of about the same thing.  The first parses out the last 3 characters.  The second parses out all characters after the first character.  The third does the same as the second but creates a numeric result channel that you can use as an X axis on a graph.  The double-double-quote is the escape sequence in VBScript to denote one double quote character (ASCII=34) inside of a string constant.

 

Call ChnCalculate("Ch(""[1]/Result"") = Right(Ch(""[1]/Frame""), 3)")
Call ChnCalculate("Ch(""[1]/Result"") = Mid(Ch(""[1]/Frame""), 2)")
Call ChnCalculate("Ch(""[1]/NumericResult"") = CDbl(Mid(Ch(""[1]/Frame""), 2))")

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 2 of 8
(6,889 Views)

But it is automatic every cells in the same column at once.  I want to know how go through each single cell by DIAdem scripting. 

0 Kudos
Message 3 of 8
(6,886 Views)
Solution
Accepted by topic author NiCoder

Hi NiCoder,

 

This is how you read and write individual channel values, but know that doing it value by value is much slower than doing them all with one ChnCalculate() call:

 

Set Group = Data.Root.ChannelGroups(1)
Set FrameChannel = Group.Channels("Frame")
Set ResultChannel = Group.Channels.Add("Result", DataTypeString)
iMax = FrameChannel.Size
FOR i = 1 TO iMax
  ResultChannel(i) = Right(FrameChannel(i), 3)
NEXT ' i

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

Message 4 of 8
(6,881 Views)

How to combine all the following code into one call use ChnCalculate() function

 

Dim FrameStringSize

Set Group = Data.Root.ChannelGroups(1)
Set FrameChannel = Group.Channels("Frame")
Set ResultChannel = Group.Channels.Add("Result", DataTypeString)
iMax = FrameChannel.Size
FOR i = 1 TO iMax

   FrameStringSize = Len(FrameChannel(i))

   if (FrameStringSize = 7)
     ResultChannel(i) = Right(FrameChannel(i), 3)

   else if (FrameStringSize = 😎

     ResultChannel(i) = Right(FrameChannel(i), 4)

   End If
NEXT ' i

 

0 Kudos
Message 5 of 8
(6,852 Views)

Hi NiCoder,

 

None of the "Frame" values in the data set you posted have more than 4 characters, so this script you posted doesn't seem to apply to that data.  I had to tweak your syntax a bit to get it to run-- here it it:

 

Set Group = Data.Root.ChannelGroups(1)
Set FrameChannel = Group.Channels("Frame")
Set ResultChannel = Group.Channels.Add("Result", DataTypeString)
iMax = FrameChannel.Size
FOR i = 1 TO iMax
   FrameStringSize = Len(FrameChannel(i))
   if (FrameStringSize = 7) THEN
     ResultChannel(i) = Right(FrameChannel(i), 3)
   elseif (FrameStringSize = 8) THEN
     ResultChannel(i) = Right(FrameChannel(i), 4)
   ELSE
     ResultChannel(i) = FrameChannel(i)
   End If
NEXT ' i

 If you submit a script and data set that make sense to gether, I'll be happy to send you an example of using the IIF() function in the ChnCalculate() expression to change a script like that above into one line of code that executes much faster.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 6 of 8
(6,840 Views)

Sorry, they should be column word_XX data in the same file.

0 Kudos
Message 7 of 8
(6,822 Views)

Hello NICoder,

 

may be, I don't get the challenge here but wouldn't the Mid() function do the job ? From the scripts I saw, you try to copy all characters right of the  4th one, right ?

In that case the script could be something like :

 

Dim oGroup,oTextChannel

Data.Root.Clear

Set oGroup = Data.Root.ChannelGroups.Add("MyGroup")

Set oTextChannel = oGroup.Channels.Add("MyText",DataTypeChnString)

oTextChannel(1) = "Peter A"

oTextChannel(2) = "Paul BCD"

oTextChannel(3) = "Mary EFG"

oTextChannel(4) = "Jay"

' the following is the calculation itself. Omitting the second parameter of Mid() makes VBScript copying the "rest"

' Shorter text like "Jay" creates an empty entry which is probably what you want in this case

Call ChnCalculate("Ch(""LastCharacters"") = Mid(Ch(""MyText""),5)")

 

 

 

0 Kudos
Message 8 of 8
(6,803 Views)