![]() | Download Help (Windows Only) |
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. A LabVIEW MathScript function must use the following syntax:
function outputs = function_name(inputs)The following example of a user-defined function returns both the sum and the difference of two input values:
function [sum, difference] = sumdifference(a, b)
%Finds the sum and the difference of a and b
sum = a + b;
difference = a - b;
For this example, you must save the function as sumdifference.m. The filename for a function must be the same as the name of the function and must have a lowercase .m extension. You can use the following syntax to call sumdifference.m from the LabVIEW MathScript Window or a MathScript Node:
[sum, difference] = sumdifference(5, 9)
The following examples show the valid function signatures for foo, where foo represents a user-defined function:
function foo | function a = foo | function [a b] = foo |
function foo() | function a = foo() | function [a b] = foo() |
function foo(g) | function a = foo(g) | function [a b] = foo(g) |
function foo(g, h) | function a = foo(g, h) | function [a b] = foo(g, h) |
If you define multiple functions in one MathScript file, all functions following the first are subfunctions and are accessible only to the main function. You cannot call functions recursively. For example, foo cannot call foo. LabVIEW also does not allow circular recursive function calls. For example, foo cannot call bar if bar calls foo.
The following is an example of a MathScript file that uses proper syntax. fadd3 is the main function, and add2 is a subfunction of fadd3.
function a = fadd3(x)
% fadd3 adds 3 to the input value x.
a = 1 + add2(x);
function a = add2(x)
% add2 is a subfunction. Only fadd3 can call this function.
% add2 computes the constant 2 using Euler's formula:
% e^(i*theta) = cos(theta) + i*sin(theta)
a = x - (exp(i*pi) - 1);
You can specify optional inputs and outputs for a function. Use the arginnum function to determine the number of input arguments supplied to the function that calls arginnum. If the number of inputs supplied is less than the maximum number of inputs for the function, you can assign default values to define the optional inputs. Use the argoutnum function to determine the number of output arguments requested from the function that calls argoutnum. If the number of outputs requested is less than the maximum number of outputs for the function, you can bypass the calculation of the unrequested outputs.
Helpful
Not Helpful