Command: Calculate
- Updated2023-02-21
- 7 minute(s) read
Command: Calculate
Uses the Calculator formula syntax to calculate a formula in a script including the units.
Input Parameters
CalculateFormula |
Specifies a formula in a script. Enter the formula in quotation marks. You must enclose texts that are in quotation marks, such as channel names, in double quotation marks. |
[CalculateSymbols] |
Specifies a list with symbol names, which are replaced in the formula with values. The symbols CalculateSymbols and the values CalculateValues are assigned through their field index. |
[CalculateValues] |
Specifies a list of the values associated with the symbols. The symbols CalculateSymbols and the values CalculateValues are assigned through their field index. |
[CalculateTargetUnit] |
Specifies the unit symbol for the result of the calculation. If you do not specify a unit symbol, DIAdem specifies the unit symbol. |
Specify the formula as follows: Result = Expression.
![]() | To execute a formula calculation based on quantities, assign the value TRUE to the CalcQuantityBased variable. When executing quantity-based calculations, DIAdem checks whether the program can also execute calculations on physical quantities in the same calculation. If DIAdem cannot execute the calculation because the physical quantities are unsuitable, the program displays an error message. Refer to the Help page Calculating Quantity-Based and Non-Quantity-Based in DIAdem for the conditions and rules for quantity-based calculations. In addition to the rules for quantity-based calculations the following rules apply for the calculator and for the Calculate command:
|
Examples (Script)
The following example doubles each value of the Group1/Input data channel and saves the result in the Group2/Result data channel.
| VBScript | Python |
Call Calculate("Ch(""Group2/Result"")= 2 * Ch(""Group1/Input"")")
The following example exponentiates the values of the Group1/Input data channel to the base e (Eulerian number) and saves the result in the Group3/Result data channel.
| VBScript | Python |
Call Calculate("Ch(""Group3/Result"")= Exp(Ch(""Group1/Input""))")
The optional parameters CalculateSymbols and CalculateValues replace symbols with values in formulas. Both fields must be the same length.
In the following example DIAdem replaces the symbols A and B with the numbers 2 and 3, and assigns the result of the calculation A/B to the variable R1.
| VBScript | Python |
Dim sFormula, aSymbol(2), aValues(2) sFormula = "R1 = A / B" aSymbol(1) = "A" aSymbol(2) = "B" aValues(1) = 2 aValues(2) = 3 Call Calculate (sFormula, aSymbol, aValues) Call MsgboxDisp(R1)
In the following example DIAdem replaces the symbols A and B with the channels [1]/[2] and [1]/[3], and assigns the result of the A/B calculation to the Result channel.
| VBScript | Python |
Dim sFormula, aSymbol(2), aValues(2) sFormula = "Ch(""Result"") = A / B" aSymbol(1) = "A" aSymbol(2) = "B" Set aValues(1) = Data.GetChannel("[1]/[2]") Set aValues(2) = Data.GetChannel("[1]/[3]") Call Calculate (sFormula, aSymbol, aValues)
In the following example DIAdem replaces the symbol A with the channel [1]/[2], and replaces the symbol B with the text "Example Text". DIAdem assigns the result of the calculation A/len(""B"") to the Result channel. This means that DIAdem calculates the length of the "Example Text" text and divides all values of the [1]/[2] channel by this number.
| VBScript | Python |
Dim sFormula, aSymbol(2), aValues(2) sFormula = "Ch(""Result"") = A / len(""B"")" aSymbol(1) = "A" aSymbol(2) = "B" Set aValues(1) = Data.GetChannel("[1]/[2]") aValues(2) = "Example Text" Call Calculate (sFormula, aSymbol, aValues)
In the following example, DIAdem adds 10 m (meter) and 2 cm (centimeter). DIAdem calculates in (inch) in the first half of the example, in the second half DIAdem calculates the result in the default unit of the associated physical quantity length.
| VBScript | Python |
CalcQuantityBased = TRUE Call Calculate("r1=vu(10,""m"")+vu(2,""cm"")",,,"in") Call MsgboxDisp("Result: " & R1 & VBCrLf & "Unit: " & CalculateResultUnit) Call Calculate("r1=vu(10,""m"")+vu(2,""cm"")","default") Call MsgboxDisp("Result: " & R1 & VBCrLf & "Unit: " & CalculateResultUnit)
In the following example, DIAdem replaces the symbols A and B with the value-unit object and assigns a new value-unit object A+B to the result of the calculations. In the following example, DIAdem adds 10 m (meter) and 2 cm (centimeter). DIAdem calculates the result in the default unit of the associated physical quantity length.
| VBScript | Python |
Dim sFormula, aSymbol(3), aMyValueUnit(3) sFormula = "C= A + B" aSymbol(1) = "A" aSymbol(2) = "B" aSymbol(3) = "C" Set aMyValueUnit(1) = CreateValueWithUnit(10, "m") Set aMyValueUnit(2) = CreateValueWithUnit(2, "cm") Set aMyValueUnit(3) = CreateValueWithUnit(null, "") Call Calculate (sFormula, aSymbol, aMyValueUnit, "default") Call MsgboxDisp("Result: " & aMyValueUnit(3).Value & VBCrLf & "Unit: " & aMyValueUnit(3).UnitSymbol)
Related Topics
Command: ChnCalculate | Command: FormulaCalc | Connecting Channels and Values in Calculations | Creating a Calculation Script with Debug Option | Creating and Executing Calculations | Executing Calculations Multiple Times | Executing Channel Calculations in the Calculator | Repeating Calculations Automatically | Running Comparisons on the Calculator | Using Channel Lists in Calculations | Variable: CalcQuantityBased | Working with Dependent Inputs | Working with Text in the Calculator


