DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing String Variable To Calculate Function

Solved!
Go to solution

Hey guys, I am having a challenge with passing an argument within DIAdem to the calculate function.  I think it might be something to do with the way vbscript handles it and I am just not familiar enough to make it work.  

 

Context: My client gets data dumps daily for their assets with 900+ channels per unit. Typically, each unit is the same data file and structure, but some groups log variables that others don't, so we needed to make a modular system to reduce each data set to a easily digestible trend. We tried to implement a modular approach where the "data reduction" and "plotting" are handled by two separate scripts.  The plotting script reads from a spreadsheet to know which channels to plot and on which graph (which works great).

 

What we cannot do, is process basic channel functions from the spreadsheet.

 

The Problem:

We are trying to read in a cell that lists ChanA <operand> ChanB which we want to pass to the calculate function.  I am able to parse the cell value and create a proper string.  I can copy that string and paste it into the calculate function and run it successfully.  I just cannot pass the string value into calculate.  It always returns the error "Cannot assign the calculation result to the left side of the formula."

 

My Code mocked up to use the example data:

Dim CellCalc
CellCalc = "(Noise_1) - (Noise_2)"
Dim Par1 : Par1 = InStr(CellCalc, ")")
Dim Operation : Operation = Mid(CellCalc,Par1 + 2,1)
Dim Chan1 : Chan1 = Mid(CellCalc,2,Par1 - 2)
Dim Chan2 : Chan2 = Mid(CellCalc, Par1 + 5, len(CellCalc) - Par1 - 5)
Dim Formula
Formula = """Ch(""""Results/[1]"""") = "
Formula = Formula & "Ch(""""[2]/" & Chan1 & """"") "
Formula = Formula & Operation
Formula = Formula & " Ch(""""[2]/" & Chan2 & """"")"
Formula = Formula & """" & ",NULL,NULL,"""""
Call TextToClipboard(Formula)
Call Calculate(Formula)

 

 

Thank you so much for any insight! -Gustav

0 Kudos
Message 1 of 3
(3,446 Views)
Solution
Accepted by topic author GustavHernan

Hey Gustav,

 

Here are two possible solutions for your example (I just copied your first part for extracting the channel names):

 

Dim CellCalc 
CellCalc = "(Noise_1) - (Noise_2)"
Dim Par1 : Par1 = InStr(CellCalc, ")")
Dim Operation : Operation = Mid(CellCalc,Par1 + 2,1)
Dim Chan1 : Chan1 = Mid(CellCalc,2,Par1 - 2)
Dim Chan2 : Chan2 = Mid(CellCalc, Par1 + 5, len(CellCalc) - Par1 - 5)

'Solution 1
Dim Formula
Formula = "Ch(""Results/[1]"") = " 
Formula = Formula & "Ch(""[2]/" & Chan1 & """) " 
Formula = Formula & Operation 
Formula = Formula & " Ch(""[2]/" & Chan2 & """)" 
Call TextToClipboard(Formula)
Call Calculate(Formula)

'Solution 2
Dim aSymbol(1), aValues(1)
Formula = "Ch(""Results/[2]"") = A" & Operation & "B"
aSymbol(0) = "A"
aSymbol(1) = "B"
Set aValues(0) = Data.GetChannel("[2]/" & Chan1)
Set aValues(1) = Data.GetChannel("[2]/" & Chan2)
Call Calculate (Formula, aSymbol, aValues)

In your example code there were too many quotation marks.

 

If you need quotation marks in a calculator command, e.g. for a channel reference you have to do it like this:

In Calculator: Ch("Results/[1]")

In Script with Calculate command: "Ch(""Results/[1]"")"

 

Double quotation marks in a string means there will be only one quotation mark in the resulting string that you use as a parameter.

 

Regards

Christian
CLA, CTA, CLED
Message 2 of 3
(3,427 Views)

Christian, Thank you so much!

 

I need to learn how to paste code like you did.  Makes it much more readable.  I was chasing my tail for days and you helped me in less than an hour.  Thank you so much.

0 Kudos
Message 3 of 3
(3,421 Views)