Company Events Academic Community Support Solutions Products & Services Contact NI MyNI

MathScript Syntax (MathScript RT Module)

LabVIEW 2012 MathScript RT Module Help

Edition Date: June 2012

Part Number: 373123C-01

»View Product Info
Download Help (Windows Only)

Use LabVIEW MathScript to write functions and scripts for use in the LabVIEW MathScript Window or MathScript Node. Use the following guidelines when writing MathScript functions and scripts:

  • You cannot define variables that begin with an underscore, white space, or a digit.
  • MathScript variables adapt to data types. For example, if a = sin(3*pi/2), then a is a double-precision floating-point number. If a = 'result', then a is a string.
  • You can use either i or j to represent the imaginary unit equal to the square root of –1.
  • Use white space or commas to separate matrix elements, and use semicolons to separate rows of a matrix.
  • Use single quotation marks to define a string. Use two single quotation marks to include a single quotation mark within a string. For example, if a = 'C:\test\test''s subfolder' where test''s includes two single quotation marks, then a is the string C:\test\test's subfolder.
  • When calling built-in functions, you can pass an input without parentheses if that input is a string. For example, pathgetsubset dir and pathgetsubset(dir) both return the current search path list because dir is a string that specifies a path in both syntaxes.
  • To add comments, precede each line of text with a % character.
  • Use a == b to perform an equality comparison. Use a ~= b to perform an inequality comparison. LabVIEW uses short-circuit evaluation to evaluate compound logical expressions in MathScript. For example, if you execute the command if 0 == 0 || foo(a) == 2, LabVIEW does not execute foo(a) because the first part of the expression already is TRUE. Similarly, if you execute the command if 0 ~= 0 && foo(a) == 2, LabVIEW does not execute foo(a) because the first part of the expression already is FALSE.
  • Arrays in MathScript are one-based, not zero-based. Therefore, the index of the first element in the array is one.
  • You cannot use n-dimensional arrays.
  • If you end a command line with a semicolon, the LabVIEW MathScript Window and the LabVIEW MathScript probe do not display the output for that command.
Note  Some functions, such as disp, display output even if you end the command line with a semicolon.
  • You cannot use a comma as a decimal separator. You must use a period as a decimal separator.
  • MathScript supports all non-Unicode characters in text strings but not in variable names. You can use only ASCII characters in variable names. For example, you can use á in a text string, but you cannot call the following script from the LabVIEW MathScript Window or the MathScript Node:

    á = rand(50, 1)
    plot(á)
  • You cannot execute MathScript functions directly. In other words, you first must define and save a function before you can call the function with inputs and outputs in the LabVIEW MathScript Window or the MathScript Node.

The following table provides the MathScript syntax for commonly used constructs.

ConstructGrammarExample
Switch Statementswitch expression
case expression
statement-list
[case expression
statement-list]
...
[otherwise
statement-list]
end
switch mode
case 'start'
a = 0;
case{'middle', 'ongoing'}
a = 1;
case 'stop'
a = –1;
otherwise
a = a+1;
end

When a case in a switch statement executes, LabVIEW does not select the next case automatically. Therefore, you do not need to use break statements as in C.
For Loopfor expression
statement-list
end
for i = 1:10
     a = sin(2*pi*i/10)
end
If-Else Statementif expression
statement-list
[elseif expression
statement-list]
...
[else
statement-list]
end
if b == 1
     c = 3
else
     c = 4
end
Rangestart:[step:]endb = 2:2:20
b returns the even numbers between 2 and 20.
If you do not specify a step size, LabVIEW uses a step size of 1.

You can use the end keyword within a range construct to indicate the last element of a row, column, or matrix.
A = [1 2 3; 4 5 6; 7 8 9]
A(3, 2:end) returns the elements in the third row of matrix A starting at the second element and ending at the last element.
While Loopwhile expression
statement-list
end
while i < 10
     a = cos(2*pi*i/10)
     i = i + 1;
end

 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit