Using MathScript Node in LabVIEW

Updated Sep 22, 2020

Environment

Software

  • LabVIEW
  • LabVIEW MathScript Module

Note: NI does not recommend LabVIEW MathScript Module functions for new designs. See www.ni.com/migratemathscript for information and recommended alternatives.

The MathScript Node is a structure on the LabVIEW block diagram that gives you the ability to put text-based MathScript code inline with graphical LabVIEW code. You can define inputs and outputs on the node borders to pass data back and forth between the two paradigms. The node even supports debugging, with single steps, breakpoints, syntax highlighting, and a probe for intermittent values.

The MathScript Node implements similar functions to the MathScript Window but with the additional functionality of a full .m file compiler, making it useful as a textual language for signal processing, analysis, and math. LabVIEW MathScript is generally compatible with .m file script syntax, which is widely used by alternative technical computing software. Users do not need to have access to MATLAB™ to use this structure, but users who have MATLAB .m files or are familiar with its syntax may find the MathScript Node to be of greater benefit. 

The use of MathScript Node requires the installation and activation of the MathScript Module. If you are interested in options do not require an additional license and are familiar with C++ syntax, look into using Formula Nodes in LabVIEW as an alternative.

This tutorial familiarizes users with the MathScript Node, walking through how to add basic code and interface with LabVIEW inputs and outputs. This tutorial assumes that you have basic experience working with LabVIEW. 

  1. Ensure that you have LabVIEW and the LabVIEW MathScript Module are installed before beginning.
    • Note that you must have the same version and bitness of LabVIEW and MathScript Module installed.
    • Note that MathScript Module is only compatible with Windows OS. Check the readme for your version of MathScript to determine compatible operating systems
  2. Launch LabVIEW and open a new VI.
  3. On the Front Panel, right click to add a Vertical Pointer Slide (Controls >> Numeric >> Vertical Pointer Slide) and label it Amplitude.
  4. Add a Waveform Graph (Controls >> Graph >> Waveform Graph) named Sinusoid and FFT Analysis.
  1. On the Block Diagram, right-click to add a MathScript Node by navigating to Functions >> Structures >> MathScript.
  2. Add the structure to the Block Diagram by clicking, dragging, and releasing the mouse.
  3. Click inside the MathScript Node and type the following commands.
x = linspace(0, 6*pi, 100);
b = amp*sin(x);
Note that the x = linspace(0, 6*pi, 100); command creates a new variable x and populates that variable with 100 samples evenly distributed between 0 and 6*pi.
At this point, you will have a red x next to the second line of code as we have not yet defined our input variable amp.
 
  1. Right-click the left side of MathScript Node frame and select Add Input from the shortcut menu.
  2. Type amp in the input terminal to add an input for the amp variable in the script. 
    • Note that the red x is now gone as the amp variable has been defined.
  3. Wire the Amplitude control into the input.
  4. Right-click the right side of the MathScript Node frame and select Add Output » b from the menu.
    • You’ll notice that the menu provides all of the declared variables within the MathScript Node as an option to output.
  5. Wire the output to the Sinusoid indicator.
  1. On the Front Panel, drag the slider of the Amplitude control to approximately 3.
  2. Click the Run button at the top of the front panel. Notice that the waveform graph updates with a sine wave with an amplitude of what you set in the slider.
  1. Drag the slider of the Amplitude control to a different value and run the VI again. Notice that the sine wave updates accordingly.
    • (Optional) Add a While Loop and Stop Button around the code on the block diagram. Now, the waveform graph will update continuously as you change the value of the Amplitude control.  

 

Next Steps

  • In addition to typing directly into the MathScript Node, you can import files to load your previously written scripts. As an addition to the exercise above, try saving the lines of code above in a .txt file. Then, load the script using the Import... from the MathScript Node shortcut menu. 

MATLAB is a registered trademark of The MathWorks, Inc.