LabVIEW 2012 MathScript RT Module Help
»View Product Info
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)
- 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.
|Switch Statement||switch expression|
a = 0;
a = 1;
a = –1;
a = a+1;
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 Loop||for expression|
|for i = 1:10|
a = sin(2*pi*i/10)
|If-Else Statement||if expression|
|if b == 1|
c = 3
c = 4
|Range||start:[step:]end||b = 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 Loop||while expression|
|while i < 10|
a = cos(2*pi*i/10)
i = i + 1;