Table Of Contents

Creating User-Defined Functions

    Last Modified: March 3, 2017

    Create user-defined functions to separate recurrent calculations from the main program in a MathScript Node.

    First, create the user-defined function.

    The following procedure demonstrates how to create a user-defined MathScript function and call this function in a MathScript Node using a specific example. The example returns both the sum and the difference of two input values. The steps you need to take to complete your specific task might be different than the following steps.

    1. In the Project Files tab, add a MathScript to your project.
    2. Enter the following code in the document editor:
      function [sum, difference] = sumdiff(a, b)
        %Calculates the sum and the difference of a and b
        sum = a + b
        difference = a - b
    3. Right-click the MathScript document tab and click Rename. The function name is selected.
    4. Enter sumdiff.

    Now create the MathScript Node with the MathScript code that calls the user-defined function.

    1. In the Project Files tab, add a VI.
    2. Add a MathScript Node to the diagram.
    3. Enter the following code in the MathScript Node:
      [D, E] = sumdiff(1, 2)
    4. Right-click D in the MathScript Node and select Create»Output: D.
    5. Right-click the D output on the MathScript Node frame, select Create Indicator, and click into the diagram in order to place the indicator.
    6. Repeat steps 8 and 9 for E in the MathScript Node.
    7. Click the Panel selector.
    8. Drag all of the indicators in the unplaced items tray to the panel.
    9. Click Run. The indicators show the results for D and E.

    Recently Viewed Topics