Developing Algorithms Using LabVIEW MathScript RT Module: Part 2- The LabVIEW MathScript Window
Overview
This series of documents provides exercises to introduce you to LabVIEW MathScript. This document provides a step-by-step example that shows how to use the LabVIEW MathScript Window.
Table of Contents
What is the MathScript RT Module?
With LabVIEW, you can choose the most effective syntax for developing algorithms, exploring signal processing concepts, or analyzing results. You can combine the LabVIEW graphical programming paradigm with LabVIEW MathScript, a math-oriented textual programming language.
See Also:
Inside LabVIEW MathScript: Making LabVIEW Ideal for Textual Algorithm Development and Prototyping
Multimedia Demonstration of LabVIEW MathScript
The LabVIEW MathScript Window
You can work with LabVIEW MathScript in the LabVIEW MathScript Window or the MathScript Node. Refer to Part 1 of this series (linked below) for more information about the MathScript Node.
You can use the LabVIEW MathScript Window, shown in the following figure, to enter commands one at time. You also can enter batch scripts in a simple text editor window, loaded from a text file, or imported from a separate text editor. The LabVIEW MathScript Window provides immediate feedback in a variety of forms, such as graphs and text.

- In the Getting Started window, select Tools»MathScript Window to open the LabVIEW MathScript Window.
- In the Command Window, enter 4+6+1. Then press the <Enter> key. LabVIEW displays the result of the command in the Output Window. You can enter commands one at a time for immediate feedback.
- Click the Variables tab. LabVIEW updates the variable, ans, under the Local section in the Partition/Variable tree, to contain the result of the last command executed.
- Click the Script tab and enter the following commands.
x=linspace(0,2*pi, 30);
b=sin(x)./(cos(x));
plot (x,b);axis([0 2*pi -20 20]);
Notice that the x=linspace(0,2*pi,30); command creates a new variable x and populates that variable with 30 samples evenly distributed between 0 and 2*pi. - Click the Run button on the Script page. The Plot 1 window appears and displays an XY plot of x versus b.

- Click the Variables tab to display variables you create, such as x and b.
- Select b from the Partition/Variable tree. A table representing the numeric values of b appears. Enable the Graphical First? checkbox to change the numeric display to a graph display. A graph of the results appears. When you enable the Graphical First? checkbox, you will see the graphical display of the variable's values first, rather than the default of the numerical representation.

- Click the Script tab and click the Load button. Select Mitra P2_1.m (Mitra, Sanjit and Kaiser, James H., Handbook for Digital Signal Processing [New York: John Wiley and Sons, 1993], 21). This script generates a test signal and then applies a moving-average filter to the test signal.
- Click the Run button to run the script. The Prompt User for Input dialog box appears. Enter a positive value in the Desired length of the filter = text box and click the OK button. MathScript scripts can include interactive dialog boxes to request user input.
- The Mitra P2_1.m script, shown below, uses subplot commands to specify one of the four sub-plots on the Plot window. After you specify a sub-plot, subsequent commands affect that sub-plot. For example, a plot command, following a subplot command, will populate the sub-plot specified in the subplot command with the plot specified in the plot command.
% Simulation of an M-point Moving Average Filter
% Generate the input signal
n = 0:100;
s1 = cos(2*pi*0.05*n); % A low-frequency sinusoid
s2 = cos(2*pi*0.47*n); % A high frequency sinusoid
x = s1+s2;
% Implementation of the moving average filter
M = input('Desired length of the filter = ');
num = ones(1,M);
y = filter(num,1,x)/M;
% Display the input and output signals
clf;
subplot(2,2,1);
plot(n, s1);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #1');
subplot(2,2,2);
plot(n, s2);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #2');
subplot(2,2,3);
plot(n, x);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Input Signal');
subplot(2,2,4);
plot(n, y);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Signal');
axis;
You can use the Command Window to find more information about commands. For example, type help subplot in the Command Window and a description of the command, along with information about syntax, inputs, outputs, and examples appears in the Output Window.
Related Links
Evaluate the LabVIEW MathScript RT Module
Developer Zone: Developing Algorithms Using LabVIEW MathScript: Part 1 - The MathScript Node
Reader Comments | Submit a comment »
Superb example clearly its bridging the
usual DSP scripts mostly done in
MatLab that now doable in a LabVIEW
environment - it would be good if a
hardware or IO from a script could be
demonstrated also
- Berns Buenaobra, IMI Philippines Design and Engineering. bernardino.j.buenaobra@imiphil.com - Mar 4, 2008
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
