Create Linear and Polar Plots with LabVIEW MathScript Node

Updated May 4, 2023

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.

 

LabVIEW also allows you to create .m files and work with text-based math using the MathScript Node. Script nodes combine text-based .m files with traditional LabVIEW graphical programming. Script nodes are resizable text-entry regions you can add to LabVIEW block diagrams. For a familiarizing yourself with the MathScirpt Node, follow the Using MathScript Node in LabVIEW tutorial. 

This tutorial shows you how to build a more complicated VI that displays an RF antenna pattern for a dish antenna in linear (XY) and polar plots. The example shows how to build a custom user interface to add interactivity to an algorithm you define in a script in a .m file using a MathScript Node. The example adds a control input to set the amplitude/lambda input parameter of the algorithm.

This tutorial requires LabVIEW and MathScript Module. Although familiarity with .m syntax is preferable, it is not required that you have MATLAB™ installed on your machine.

Create the User Interface

  1. Launch LabVIEW and open a new VI.
  2. Right-click on the front panel to add a Numeric >> Knob control. You will use this control to set the size and wavelength of the RF antenna.
  3. Change the name of the Knob control to Amplitude/lambda.
  4. Right-click on the front panel to add a Graph >> XY Graph
  5.  Right-click on the front panel to add a Graph >> Controls >> Polar Plot Indicator. You will use this indicator to show the RF antenna pattern as a function of the angle.
  6. Switch to the block diagram and locate the controls and indicators you created on the front panel. Organize the block diagram with the control (knob) on the left side and the indicators (the polar plot indicator and the XY graph) to the right side of the block diagram.
 

Add the MathScript Node

  1. Right-click on the block diagram to add a Structures >> MathScript Node.
  2. Copy and paste the following script into the MathScript Node. This script processes the gain of the antenna at different angles.
% Create angle vector in radians
theta = linspace(-pi/2,pi/2,1000);
u = 2*pi*a*sin(theta);

% initialize matrix
E = ones(size(u));

% Get index of non-zero values
i = find(u);

% Evaluate Antenna pattern equation
E(i) = pi*a^2*abs(2*besselj(1,u(i))./(u(i)));

% change theta to degrees units for polar plot
Out=theta.*180./pi;
  1. Right-click the left border of the MathScript Node and select Add Input from the shortcut menu.
  2. Add a to the input label, creating the variable for the script. 
  3. Connect the Amplitude/lambda control to the a input.
  4. Right-click the right border of the MathScript Node and select Add Output from the shortcut menu.
  5. From the output list available, select Out. This variable represents the angle vector for plotting purposes.
  6. Right-click the Out output terminal and select Choose Data Type >> 1D-Array >> DBL 1D from the shortcut menu to specify the data type of the Out output variable.
  7. Repeat the steps 12 through 14 for the variable. This variable represents the gain output at different angles.
  8. Right-click to add a Cluster, Class, & Variant >> Bundle function to the block diagram.
  9. Add Out and E respectively to the Bundle function; then wire the output of Bundle of the XY graph. This becomes the X and Y components of the XY graph as shown in the following block diagram.

 

Configure the Polar Plot

  1. Add a Structures >> For Loop to the block diagram.
  2. Place a Bundle function inside the For Loop.
  3. Connect the Output and E inputs to the input terminals of Bundle
    • The squares in the border of the For Loop indicate the loop is set to auto index, or process each input element separately.
  4. Wire the Bundle output to the data array input of the Polar Plot with Point Options VI.
  1. On the Polar Plot VI, right-click the Polar attributes input and select Create Control from the shortcut menu.
  2. Switch to the front panel and set the Amplitude/lambda control to 2.
  3. Configure the parameters of the polar plot:
    • Show a Log scale by pressing the log? button which is False by default
    • Display only the right half of the plot in visible section input.
At this point, you can test your VI by pressing Run. You will see the XY graph and polar plot data appear, and the VI will stop.
To run continuously, add a While Loop with Button around your block diagram code.

In the end, you will have created an XY graph and polar plot using the analysis capabilities of text-based code in the MathScript Node paired with the ease of graphical communication of LabVIEW.