DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Using ChnCalculate with text channels

Hi,

 

I have a text channel with an associated timestamp channel that contains information about how a test ran.  I would like to search through the text channel to find status messages, and the time they occor.  I know I can use a 'for' loop, but I thought the channel calculator might be faster.  however, my script is not working and I am not sure why.  I am attaching my dataset.  I am looking for the word, "Limit", and this is the script I am using:

 

If GroupIndexGet("Filtered") > 0 Then Call GroupDel(GroupIndexGet("Filtered"))
Call GetCreateGroup("Filtered")

Call ChnAlloc("Alarms", ChnLength("Messages"), 1, DataTypeString,"Text")    
Call ChnAlloc("AlarmTime", ChnLength("Messages"), 1, DataTypeString,"Text")    

Call ChnCalculate("If InStr(Ch(""Messages""),""Limit"") > 0 Then: Ch(""Alarms"") = Ch(""Messages"") : ELSE Ch(""Alarms"") = ""NONE"": End IF")
Call ChnCalculate("If InStr(Ch(""Messages""),""Limit"") > 0 Then: Ch(""AlarmTime"") = Ch(""Timestamp"") : ELSE Ch(""AlarmTime"") = ""NONE"": End IF")

 

Thank you for any help you can offer!

Julia

0 Kudos
Message 1 of 7
(5,186 Views)

Hello Julia!

 

You were trapped into a common misconception Smiley Wink! The ChnCalculate command like the old FormulaCalc always need an assignment with a left and right side in one line. No control operators like If are allowed. The result is that the solution for your task is a little bit tricky. Here is the code:

If GroupIndexGet("Filtered") > 0 Then Call GroupDel(GroupIndexGet("Filtered"))
Call GroupCreate(
"Filtered")
Call GroupDefaultSet(GroupIndexGet(
"Filtered"))
 
Call ChnAlloc(
"Alarms", ChnLength("Messages"), 1, DataTypeString,"Text")
Call ChnAlloc(
"AlarmTime", ChnLength("Messages"), 1, DataTypeString,"Text")
 
Call ChnCalculate(
"Ch(""Alarms"" ) = Array(""NONE"",Ch(""Messages"" ))(InStr(Ch(""Messages"" ),""Limit"" ))")
Call ChnCalculate(
"Ch(""AlarmTime"" ) = Array(""NONE"",Ch(""Timestamp"" ))(InStr(Ch(""Messages"" ),""Limit"" ))")

Note: Put the ChnCalculate command in one line. The forum splitted it into two!

 

You can make it easier to read if you implement a function in a global extension script.

 

Matthias

Message Edited by Twigeater on 12-09-2008 11:15 PM
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 7
(5,184 Views)

Thank you for the solution, and funny to see my code in text!  However, I do not understand what you mean by "The ChnCalculate command like the old FormulaCalc always need an assignment with a left and right side in one line".  The DIAdem help file says that I can "use single-line scripts" like the following:

If Ch("[1]/Chn1")>10 Then: Ch("[1]/Result")=Novalue: Else: Ch("[1]/Result")=0: End If"

 

I've tried using ChnCalculate with numeric channels with this syntax with mixed results (see attached file).  I know how to do this with FormulaCalc, but perhaps I have misunderstood the "Differences between VBS Syntax and AUT Syntax in the Calculator" help file.  I do not understand why my example works at all.  Either way I appreciate your solution that works!

 

Julia

0 Kudos
Message 3 of 7
(5,180 Views)

Hello Julia!

 

Sorry if I wasn't clear! What I tried to explain is that the expression you can use must be like an assignment to a variable.

Lets make an simple example without any channel calculation:

Dim MyVar1
Dim MyVar2

MyVar1 = 123

MyVar2 = MyVar1

 

MyVar2 = MyVar1 is the Assingment with a left and a right side. The left side describes where the result is stored, the right side is the expression to evaluate.

 

Lets switch to the ChnCalculate. There it is similar you have to define a left and a right like this:

Call ChnCalculate("Ch(""Alarams"") = Ch(""Messages"")")

 

The left side may only be a single channel. The right side must be evaluatable to a single scalar value.

 

To get a clue what is going on you can convert this line to an expression for one channel value. Therfore you have to exchange the Ch to an Chd. For the first value it will look like this:

Chd(1,"Alarams") = Chd(1,"Messages")

 

Internal in DIAdem the ChnCalculate will do this for every value in 'Messages'. You can see again that the right side must evalateable to an scalar value. A If control structure will not work.

 

Was I clear now? If not please feel free to ask again!

 

Matthias

Message Edited by Twigeater on 12-10-2008 11:44 AM
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 4 of 7
(5,159 Views)

It is definately becoming more clear, thank you for your extended explanation.  What I do not understand though is, you say an IF control structure will not work with ChnCalculate, but the DIAdem help says that I may use IF control structures (in the help about VBS syntax in the calculator), and when I try to use it, I get mixed results, rather than an error.  But perhaps these are questions for NI!  I greately appreciate your help!

 

Julia 

 


Twigeater wrote:

Hello Julia!

 

Sorry if I wasn't clear! What I tried to explain is that the expression you can use must be like an assignment to a variable.

Lets make an simple example without any channel calculation:

Dim MyVar1
Dim MyVar2

MyVar1 = 123

MyVar2 = MyVar1

 

MyVar2 = MyVar1 is the Assingment with a left and a right side. The left side describes where the result is stored, the right side is the expression to evaluate.

 

Lets switch to the ChnCalculate. There it is similar you have to define a left and a right like this:

Call ChnCalculate("Ch(""Alarams"") = Ch(""Messages"")")

 

 

The left side may only be a single channel. The right side must be evaluatable to a single scalar value.

 

To get a clue what is going on you can convert this line to an expression for one channel value. Therfore you have to exchange the Ch to an Chd. For the first value it will look like this:

Chd(1,"Alarams") = Chd(1,"Messages")

 

 

Internal in DIAdem the ChnCalculate will do this for every value in 'Messages'. You can see again that the right side must evalateable to an scalar value. A If control structure will not work.

 

Was I clear now? If not please feel free to ask again!

 

Matthias

Message Edited by Twigeater on 12-10-2008 11:44 AM

 

0 Kudos
Message 5 of 7
(5,151 Views)

Hi Julia,

 

I have never tried to get the ChnCalculator() to execute a free-form VBS IF-THEN-ELSE statement on one line, and I'm surprised that the Help system advocates it.  I'm even more surprised that they suggest the syntax to iinclude colons ( : ), which would not run in a VBScript command line.

 

In DIAdem 11.0 there is a new function called "IIF()" which you can use in Channel Calculator expressions exactly the way you would use the "IF()" function in Excel.  This approach seems to do what you want on my computer:

 

Call ChnCalculate("Ch(""Alarms"") = IIF(InStr(Ch(""Messages""),""Limit"") > 0, Ch(""Messages""), ""NONE"")")

Call ChnCalculate("Ch(""AlarmTime"") = IIF(InStr(Ch(""Messages""),""Limit"") > 0, Ch(""Timestamp""), ""NONE"")")

 

Brad Turpin
DIAdem Product Support Engineer
National Instruments

Message 6 of 7
(5,146 Views)

Hi Julia,

 

The syntax that is described in the help does not work. 

You can use Brads suggestion with the IIF function. We are sorry about the confusion and will change the example in the help.

 

Thanks

 

Winfried

0 Kudos
Message 7 of 7
(5,120 Views)