DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Channel calculation with if loop

Solved!
Go to solution

 

Hello!

 

I got a syntax problem concerning the channel calculation (I have to admit I'm not an expert at all...).

Here is the Problem: I got a measurement with two groups and several channels. I have to create a new channel that switches to 1 if the right conditions are given and goes zero if they are not fullfilled. My problem is that I don't know how to execute a calculation and how to adress the channels within an if-loop.

It should look somehow like this:

 

Call BadExample

Function BadExample(NewCreatedChannel)

If 

("Ch('[2]/Example')>= 1000) and ("Ch('[2]/Example2') < 1600) and ("Ch('[2]/Example3') > 10) and ("Ch('[2]/Example4')< 0.025*("Ch('[2]/Example5')+ 10) and ("Ch('[2]/Example6') > 210)

or

("Ch('[2]/Example')>= 1200) and ("Ch('[2]/Example2') < 1800) and ("Ch('[2]/Example3') > 10) and ("Ch('[2]/Example4')< 0.025*("Ch('[2]/Example5')+ 10) and ("Ch('[2]/Example6') > 240)

then

 NewCreatedChannel=1

Else

 NewCreatedChannel=0

End if

End Function

 

I know that this won't work...but I hope you'll get the idea!

It would even help me if someone could tell me how to implement a calculated channel within this loop.

 

Thanks!!!!

0 Kudos
Message 1 of 5
(5,475 Views)
Solution
Accepted by topic author Melihson

Hey Melihson,

 

there are many possibilities to do this.

One example is to use the calculator function IIF that works with channels as well.

You just have to declare the condition and to choose the true and false values which can be 0 and 1.

Here's a simple example using the Example.TDM file:

 

Call Data.Root.Clear()
Call DataFileLoad("EXAMPLE.TDM","TDM","Load")

Call Calculate("Ch(""Results/Res1"") = IIF(Ch(""[2]/Schall_1"")>0.1, 1, 0)")
Call Calculate("Ch(""Results/Res2"") = IIF(Ch(""[2]/Schall_2"")>0.4, 1, 0)")
Call Calculate("Ch(""Results/ResAnd"")= AndB(Ch(""Results/Res1""),Ch(""Results/Res2""))")

If you're using the english version just change the channel names.

This code snipped evaluates two channels and combines them logically with AndB function.

You can also use this approach for more than two channels.

 

Best regards

Christian

Christian
CLA, CTA, CLED
Message 2 of 5
(5,460 Views)
Solution
Accepted by topic author Melihson

If you want to index channel data in a loop you can also use this approach which does exactly the same:

 

Call Data.Root.Clear()
Call DataFileLoad("EXAMPLE.TDM","TDM","Load")

const Chn1 = "[2]/Schall_1" 'channel 1
const Chn2 = "[2]/Schall_2" 'channel 2
dim i, length, ChnRes, ChnResNo, Chn1No, Chn2No

length   = CL(Chn1) 'get channel length
ChnRes   = ChnAlloc("Result",length,1,DataTypeFloat64) 'allocate result channel memory for better performance
ChnResNo = CNo(ChnRes(0)) 'get result channel number
Chn1No   = CNo(Chn1) 'get channel 1 number
Chn2No   = CNo(Chn2) 'get channel 2 number
for i=1 to length
  'ChD for channel read and ChDx for channel write without updating properties everytime
  if ChD(i,Chn1No)>0.1 and ChD(i,Chn2No)>0.4 then
    ChDx(i,ChnResNo) = 1
  else
    ChDx(i,ChnResNo) = 0
  end if
next
'update channel properties
Call ChnPropSet(ChnResNo, "length", length)
Call ChnCharacter(ChnResNo) 

Regards

Christian

Christian
CLA, CTA, CLED
Message 3 of 5
(5,454 Views)
Hey Christin,

thank you very much! You helped me a lot. Now I understand how to approach this problem and can transfer it for further solutions. Thanks again!
Regards,
Melihson
0 Kudos
Message 4 of 5
(5,439 Views)

Hi All,

 

FYII, the Calculate() command will always execute much faster than an explicit FOR NEXT loop and ChD() or Channel.Values() data access.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

Message 5 of 5
(5,434 Views)