Showing results for 
Search instead for 
Did you mean: 

Subtract a constant from values in a channel?

I need to subtract a constant from the values in a channel. Simple task in LV, but I can't see how to do it in DIAdem. The only way I can think to do it is to do the subtraction in a FOR loop - not very elegant. I tried using the calculator in the analysis form, but I can't see how to do it there. It seems like scaling an array should be a built in analysis function, but I don't see it. Please help.

0 Kudos
Message 1 of 14
Hello George,

you were right. The calculator is the solution. It is much faster than the for loop!
Here is an example how to remove the offset of channel 3:
ch(#):= ch(3) -chd(1,3)
Remind that you would habe to use this syntax if you want to use the function in a VBScript:
Call FormulaCalc("ch(#):= ch(3) -chd(1,3)")
Ingo Schumacher
Systems Engineering Manager CEERNational Instruments Germany
Message 2 of 14
Thanks that works great. But now how do I refer to the channel just created? I'm trying to find a command or variable that refers to the last channel or at least to the total number of channels.

0 Kudos
Message 3 of 14
Hi George,

You can use the variable ChnNoMax to access the last channel you created. This variable specifies teh maximum channel number. Therefore, it will correspond with the channel that you just created.

National Instruments
Caroline Tipton
Data Management Product Manager
National Instruments
0 Kudos
Message 4 of 14
Argh.. one more problem. The following FormulaCalc function works fine (the "Flow" and "Weight" channels exist in my data group):

Call FormulaCalc("ch(Flow):= ch(Weight)-chd(1,Weight)")

But I want to add the group number to the channel name and access it by a variable (since the group number I'm working on can change). The following works with a constant for the group number:

Call FormulaCalc("ch('[1]/Flow'):= '[1]/Weight'")

I've tried replacing the "[1]" with a dozen different possibilities, but I always get a syntax error.

So how in the world do you access a channel with the Group/Channel syntax in the FormulaCalc statement?

0 Kudos
Message 5 of 14
Here is how should change the line of your code:
Call FormulaCalc("ch('["&gr&"]/Flow'):= '["&gr&"]/Weight'") .
Assumed, that in gr you store your groupnumber. The little secret is that FormulaCalc needs a string as a parameter. If you need to use variables in the expression you will have to create the string as shown above. The symbols & or + concetanate strings, functions like str or cstr can be used to convert a number into a string. In the expression above, this is done implicitely.
Ingo Schumacher
Systems Engineering Manager CEERNational Instruments Germany
0 Kudos
Message 6 of 14
I forgot to mention:
ch(#):= creates a new channel in the data area, since you did not specify a name to it it will be automatically asigned. Since you dont know the channelnumber it might get difficult to reference that channel later on. There are two possibilities to work around that problem: Firstly, as you did, call the command with a channelname that you know is unique in that goup. Afterwards, you can reference the channel by its name. Secondly, you could run the command L1=cno("free") prior to do the calculation. This expression finds out the number of the next empty channel and assigns it to L1 - and this is exactly the channel formulacalc will be using to store the results.
There is another variable, that works in most situations: GlobUsedChn gives you the number of channels that are in use. In most cases this variable will give you the number of the channel you created latest. ChnNoMax in contrast gives you the number of channels currently reserved. Depending on the settings in your memory management it will show you the amount of channels allocated in memory - allthough they might be unused at the moment.
Ingo Schumacher
Systems Engineering Manager CEERNational Instruments Germany
0 Kudos
Message 7 of 14
Hi George,

One more thought on this thread. I like to get the channel just created by providing the group index and asking for the last channel created in that group. I find this both simple and robust. Since the FormulaCalculator ALWAYS creates new channels in the default Group, you can just use the variable GroupDefaultGet for the group index, as in the following code:

ChNum = CNoXGet(GroupDefaultGet, GroupChnCount(GroupDefaultGet))

Brad Turpin
Product Support Engineer
National Instruments
0 Kudos
Message 8 of 14
Step by step we're getting closer. I also need to subtract a variable (Rate - obtained in a SUD) from the weight. So I was thinking I could just do the following (Rate is declared as a global):

Call FormulaCalc("ch('["&gr&"]/Flow'):= '["&gr&"]/Weight' - Rate") .

But of course that doesn't work. There must be some way to do it.

Also could someone tell me where I can look up the syntax of how the above statement works? I can't find anything that describes when to use single quotes versus double quotes or why you're supposed to put quotes around some things.

0 Kudos
Message 9 of 14
Hi George,

I'm guessing you declared your global DIAdem variable "Rate" with the "GlobalDim()" function. These global variant variables have many advantages, but one of their disadvantages is that they can not be used directly in a FormulaCalculator() expression, because they are not strictly datatyped as a number. You have two choices. You can either assign the value of your "Rate" variable to R1 and use R1 in the FormulaCalculator() expression, or you can use an OdsValAlloc() command to create a global DIAdem variable of type DBL.

Call GlobalDim("Rate")
Rate = 5.0
R1 = Rate
Call FormulaCalc("ch('New'):= ch(1) + R1")


Call OdsValAlloc("RateVal", "ODS_TYPE_DOUBLE", 0)
RateVal = Rate
Call FormulaCalc("ch('New'):= ch(1) + RateVal")

Regarding the FormulaCalculator() syntax, it is actually quite flexible, so you have a number of options. You can either use double quotes (") or single quotes ('), but single quotes are easier to parse programmatically from a VBScript. You must have quotes around the channel name of the channel receiving the calculation. I always put "Ch()" around all my channel references, but that isn't really necessary on the right side of the equal sign (:=). I believe it's always necessary on the left side of the equal sign.

For more information, go to the DIAdem Help system, type in "formula calculator" in the "Index" tab, then select "Calculating in a script" and then either of the two links in the first "Note" (on the right).

Let us know if you have additional questions,
Brad Turpin
DIAdem Product Support Engineer
National Instruments
Message 10 of 14