Table Of Contents

Calling MATLAB® Functions and Scripts

Last Modified: November 26, 2019

Create an Interface for MATLAB to define calls to a MATLAB function or script.

  1. On the Project Files tab, select New» Interface for MATLAB® .
  2. In the Interface for MATLAB, use one of the following options to specify a function or script you want to call:
    • Click the ... button and select a MATLAB file on disk.
    • Enter the name of a MATLAB function or the filename of a MATLAB file on the MATLAB search path. Use this option to build your G dataflow application into an executable (.exe).
  3. On the Document tab, choose the File type of the MATLAB file you want to call.

    NI recommends that you format MATLAB programs into functions because functions perform better than scripts and offer a clean programming model.

  4. Define the interface node that maps to the arguments in the function or variables in the script.
    1. Click Add interface node.
    2. On the Item tab, enter a name for the interface node. The name will show up in the node icon.
    3. Click Add parameter.
    4. On the Item tab, specify the parameter name, data type, and behavior.
    5. Add more parameters as necessary. The number of parameters in the interface node must match the number of arguments in the function or the number of variables in the script.
  5. Save the Interface for MATLAB.
  6. Open the VI in which you want to call the MATLAB function or script.
  7. On the diagram palette, click Project Items» Software to find the interface node you defined in the Interface for MATLAB.
  8. Drop the interface node on the diagram.
  9. Wire the interface node and complete the diagram.
  10. Run the VI.

    The MATLAB Command Window automatically launches. Input data passes from the diagram to MATLAB, and data returns from MATLAB to the diagram.


    If you have multiple versions of MATLAB installed, by default the Interface for MATLAB invokes the version of MATLAB you most recently installed.

Recently Viewed Topics