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

Calling User-Defined Functions from LabVIEW MathScript (MathScript RT Module)

LabVIEW 2012 MathScript RT Module Help

Edition Date: June 2012

Part Number: 373123C-01

»View Product Info
Download Help (Windows Only)

After you define and save a function, you can call the function you defined from the LabVIEW MathScript Window or the MathScript Node. To call a user-defined function, the filename for the 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. You also must save the function in a directory in the MathScript RT Module search path list. The following example of a user-defined function adds two input values and returns the sum:

function return_value = add2(a, b)

%Add a and b

return_value = a + b;

You can use the following syntax to call this function after you save the file as add2.m in a directory in the search path list:

c = add2(5, 9)

Note  Calls to user-defined functions are case sensitive on Linux but not on Windows or Mac OS X.

Calling User-Defined Functions at Load Time

LabVIEW identifies user-defined functions by filename. When you call a user-defined function from the LabVIEW MathScript Window, LabVIEW searches the MathScript search path list from top to bottom for a .m file with the specified name. When you load a VI with a MathScript Node that calls a user-defined function, LabVIEW looks in the following three places, in order, for a .m file with the specified name:

  • First, LabVIEW looks to see if a .m file with the specified name is currently in memory.
  • Second, LabVIEW looks in the directory where your .m file was located when you last saved the VI that contains the MathScript Node. Because LabVIEW looks in the directory where your .m file was located when you last saved the VI, you do not need to reconfigure the MathScript RT Module search path list when you open the VI on a different computer. However, you must maintain the same relative path between the VI and your .m file.
  • Third, LabVIEW searches the MathScript RT Module search path list from top to bottom. If you open the VI on a different computer, the search path list might change because LabVIEW searches the search path list configured on that computer, not the search path list configured when you last saved the VI.
Note  If a warning glyph appears in the MathScript Node, LabVIEW looks in the directory where your .m file was last located, searches the MathScript search path list, and then looks in memory for your .m file.

Calling User-Defined Functions at Edit and Run Time

When you write a new script that calls a user-defined function, or if you add or edit a call to a user-defined function in an existing MathScript Node, LabVIEW looks to see if a .m file with the specified name is currently in memory and then searches the MathScript RT Module search path list from top to bottom. In other words, if you load an existing VI and edit a function call in a MathScript Node, LabVIEW does not look for your .m file in the directory where it was located when you last saved the VI.

Note Note  If a warning glyph appears in the MathScript Node, LabVIEW searches the MathScript search path and then looks in memory.

How LabVIEW Links to User-Defined Functions

If a .m file with the specified name is currently in memory, LabVIEW links the function call to that .m file for execution purposes. The link between the function call and the .m file behaves the same way as links between VIs and subVIs. When you try to reference a subVI at the same time another VI with the same name as the subVI is already in memory, LabVIEW references the VI already in memory instead of the subVI you selected. In the same way, if you try to load references to two different .m files with the same name from the same application instance, only one .m file is actually loaded into memory. Because both references to the .m file point to the same file, a problem known as cross-linking occurs.

To avoid cross-linking, use LabVIEW projects. When you reference a .m file from a VI that is part of a project, LabVIEW adds the .m file to the dependencies for the target. You then can configure a unique search path for the .m file. For example, you can build a VI with a MathScript Node that calls a user-defined function named foo that project1.lvproj owns. You also can build a VI with a MathScript Node that calls a different user-defined function named foo that project2.lvproj owns. Right-click a target in a project, select Properties from the shortcut menu, use the MathScript: Search Paths page in the Category list to configure a unique search path for each project, then save the VIs. When you next open one of the VIs outside of the project, LabVIEW links the call to the foo function to the appropriate .m file as long as you open only one of the VIs.


 

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