Table Of Contents

Calling MATLAB® Functions and Scripts

Version:
    Last Modified: August 24, 2018

    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. If you are calling a function, verify that the Function? checkbox on the Document tab is enabled. If you are calling a script, disable the Function? checkbox. 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.

      spd-note-note
      Note  

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


    Recently Viewed Topics