Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

MathScript Function Syntax

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

You can define functions to use in the LabVIEW MathScript Window or in the MathScript Node.

Caution  If you define a function with the same name as a built-in LabVIEW MathScript function, LabVIEW executes the function you defined instead of the original MathScript function. When you execute the help command, LabVIEW returns help content only for the function you defined. You cannot retrieve the help content for the original MathScript function.

If you define a function with the same name as a built-in LabVIEW MathScript function and want to revert to the original MathScript function, either delete the function you defined, rename the function you defined to a name that is different from the built-in MathScript function, or use the MathScript: Search Paths Options page to remove the path that contains the function you defined. After you remove the path, LabVIEW no longer loads the function you defined into memory because the list LabVIEW searches does not include the path to the function.

A LabVIEW MathScript function must use the following syntax:

function outputs = function_name(inputs)
% documentation
script

Begin each function definition with function. outputs lists the output variables of the function. If the function has more than one output variable, enclose the variables in square brackets and separate the variables with white space or commas. function_name is the name of the function you want to define. inputs lists the input variables to the function. Use commas to separate the input variables. documentation is the help content that you want LabVIEW to return for the function when you execute the help command. Precede each line of help documentation with a % character. You can place help content anywhere in the function definition. However, LabVIEW returns only the first comment block in the Output Window. You can use all other comment blocks for internal documentation. script defines the executable body of the function.

Note Note  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 are examples of valid function signatures for the foo 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 nargin function to determine the number of input arguments supplied to the function that calls nargin. 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 nargout function to determine the number of output arguments requested from the function that calls nargout. 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.

After you define a function, save the function. The filename for a function must be the same as the name of the function and must have a lowercase .m extension. For example, the filename for the foo function must be foo.m. Use unique names for all functions and scripts.


Resources


 

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