Related Content

Contact Information

Viewpoint Systems, Inc.
800 West Metro Park
Rochester, NY 14623
Phone: 585.475.9555
Fax: 585.475.9645

Viewpoint Data Management, LLC.
800 West Metro Park
Rochester, NY 14623
Phone: 585.475.9555
Fax: 585.475.9645

round top

bulletViewpoint News, Spring 2008


Tips and Techniques: New Calculations without Recompiling

In the last issue, we presented some tips to improve the flexibility of Queues through variants. In this issue, we cover a question I’ve heard many times at seminars: what’s the best way to update calculations without recompiling the application?

Many applications perform calculations. Sometimes acquired data need to be converted to a physical parameter from one or more voltage measurements. Or a known nonlinearity must be removed during a calibration step. Or the result of a computation is output to an analog or digital channel. Often these calculations are not known during development of the application program and will need to change as a function of time, part under test, or measurement sensor.

Dynamic VIs

A usual choice for this requirement is to develop the code in a VI and embed a dynamic VI call into the application. The particular VI called is specified in a string that holds the file name of the VI. When you need to have the application evaluate a different calculation, simply reset the string to the desired VI name (via a configuration file or prompt).

MathScript

Another choice utilizes the new MathScript capabilities first appearing in LV 8. For those not familiar with MathScript, this scripting feature is very similar to Matlab in both syntax and function naming.

The LV code snippet below shows how this technique works for a single argument function. The box is a MathScript node, which is found at ‘Programming > Structures’. There are two lines in the MathScript node. The first uses the ‘eval’ command. The ‘eval’ command evaluates the string argument as if it was a command and returns the result. For example, the line “a=2+3” in the MathScript node gives the same result as “a=eval(‘2+3’)”.

Since ‘eval’ takes a string, an arbitrary command can be executed by passing the command to execute in the string input. Thus, in the example below, the ‘Script’ string contents specify the command to execute in the MathScript.

The ‘feval’ command in the second line is commented out, but is included to show that it doesn’t require building the complete argument string. The “warning” icon in the upper right indicates that syntax checking is unable to validate the script, obviously because part of the script is a string!

Suppose that ‘Script’ equals “sin”. This snippet will return the value of ‘sin(x)’. Suppose that ‘Script’ equals “MainScript”. This snippet will return the value found by executing ‘MainScript.m’. For example, MainScript.m might call another M script and multiply that result by 3:

function y = MainScript(x)
y = 3*SubScript(x);

If you use M scripts, the path to the M file must be declared. Use Tools>>Options… to open the following dialog and add the appropriate Search Path(s).