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

Calling LabVIEW VIs from The MathWorks Inc. MATLAB® software environment

27 ratings | 4.07 out of 5
Print

Overview

The NI LabVIEW Math Interface Toolkit (MIT) gives you a seamless link for calling LabVIEW applications (Virtual Instruments / VIs) from The MathWorks, Inc. MATLAB® software environment.

What is the Math Interface Toolkit?

The NI LabVIEW Math Interface Toolkit (MIT) gives you a seamless link for calling LabVIEW applications (Virtual Instruments / VIs) from The MathWorks, Inc. MATLAB® software environment. 

The NI LabVIEW Math Interface Toolkit converts LabVIEW VIs into MEX-files, which is a customized DLL that the MATLAB® software recognizes. The MEX-file behaves as a native MATLAB® function while retaining the full functionality of the original LabVIEW VI, including the user interface. You can take advantage of the seamless I/O connectivity, communication capabilities, and programmatic measurement analysis functions of LabVIEW, even if you must deploy the final program in the MATLAB® software environment.

Benefits of the Math Interface Toolkit

  • Easily call LabVIEW VIs from the MATLAB® software environment
  • Convert LabVIEW VIs into MEX-files for use in the MATLAB® software environment

Target Applications and Users

LabVIEW is the industry-standard tool for acquiring, analyzing, and presenting data. The NI LabVIEW Math Interface Toolkit is recommended if you are a LabVIEW developer deploying a MATLAB® application or if you work on a team with MATLAB® developers. Using the Math Interface Toolkit, you can develop applications in the intuitive LabVIEW environment even when requirements call for a MATLAB®-based solution.

The easy-to-use LabVIEW graphical programming environment, customized assistants, drag-and-drop user interfaces, and hundreds of built-in measurement analysis functions can simplify the development of numerous types of applications. PC-based data acquisition and instrument control are two examples where LabVIEW is the ideal development environment.

PC-Based Data Acquisition

LabVIEW is the best tool for acquiring real-world data through PC-based hardware. With LabVIEW, you can take advantage of latest technologies from National Instruments, including NI-DAQmx, and all of the LabVIEW drivers developed by third-party hardware vendors. PC-based data acquisition with the MATLAB software is limited to the hardware supported by the MATLAB DAQ Toolbox.

Instrument Control

The National Instruments Instrument Driver Network (IDNet), with more than 5000 LabVIEW instrument drivers from over 200 vendors available for free download, helps make LabVIEW the best tool for instrument control. You also can use the Instrument I/O Assistant in LabVIEW to easily create applications to communicate with instruments. Instrument control with the MATLAB® software is limited to a series of low-level VISA commands the MATLAB® Instrument Control Toolbox exposes.

Converting a LabVIEW VI to a MEX-File


The Math Interface Toolkit makes it easy to create a MEX-file from a LabVIEW VI in a few simple steps.

  1. Launch the Math Interface Toolkit by selecting Tools>Math Interface Toolkit in LabVIEW.
  2. Select the VI to convert.
  3. Configure the MEX-file.
    - View the VI connector pane.
    - Name the MEX-file.
    - Configure the input parameters.
    - Configure the output parameters.
    - Select any dynamic VIs, if applicable.
    - Customize the function help.
  4. Select the MEX-file and help destination directory.
  5. Click the Create button.

The intuitive user interface uses tabs to lead you through each step.


The following example illustrates how to convert a data acquisition application into a MEX-file that you can call in the MATLAB® software environment. This example acquires a finite amount of data from one channel on a National Instruments data acquisition device. You can specify the device number and channel number on the data acquisition device, as well as the sampling rate and number of samples to read on the front panel.

[+] Enlarge Image

Viewing the VI Connector Pane

Use the VI Image tab to view the VI connector pane, which is a set of terminals that correspond to the input parameters and output parameters of the VI.

[+] Enlarge Image

Naming the MEX-File

Use the MEX-Function tab to define the name of the MEX-file as you will call it in the MATLAB® software environment. The default MEX-file name is the VI name. In this example, AIonechannel is the MEX-file name.
The function prototype shows you how to call the MEX-file in the MATLAB software environment. The function prototype changes as you configure the input and output parameters.
 

[+] Enlarge Image

Configuring the Input and Output Parameters

Use the Inputs and Outputs tab to add, remove, and reorder the input and output parameters from the connector pane for the MEX-file. A red X shows any input parameter or output parameter that is not a compatible datatype. Compatible datatypes are strings, numerics, and arrays of numerics.

This example uses the following four input parameters:

  • Device
  • Channel
  • Samples to Read
  • Sample Rate (Hz)

The order of the input parameters in the Math Interface Toolkit specifies the order in which you define the parameters in the MEX-file. This example does not use the Maximum Value and Minimum Value input parameters.


[+] Enlarge Image

In this example, Data is the only available output parameter.


[+] Enlarge Image

Selecting Any Dynamic VIs

Use the Dynamic VIs tab to add dynamic VIs to the MEX-file if the VI you are converting into a MEX-file uses VI Server to dynamically call VIs. When you add a dynamic VI, the Math Interface Toolkit automatically includes it and all its subVIs and related files, such as menu files, in the MEX-file. LabVIEW also includes DLLs if the VI references the DLL using an absolute path.

This example is not dynamically calling any VIs.

Customizing the Function Help

Use the Generated Help tab to view and customize the help for the MEX-file. The Math Interface Toolkit automatically generates help for each MEX-file you create from a VI. This automatically generated help includes the function prototype, VI description, and input and output parameter information, including the descriptions and data types.

After you create the MEX-file, you can access the function help in the MATLAB® software environemnt by typing help functionName at the MATLAB® command line, where functionName is the name of the MEX-file. The help file has the same filename as the MEX-file, but with a .m extension.


[+] Enlarge Image

Specifying the Destination Directory and Creating the MEX-File

You must select a destination directory for the MEX-file and corresponding help files that you create. Click the Create button to complete the conversion.

Calling the MEX-File from the MATLAB® software environment

The MEX-file and corresponding help file must be in the MATLAB® software environment search path. You call the MEX-file in the same way you call any other function in the MATLAB® software environment.

In this example, you can call the AIonechannel function and perform a data acquisition operation with the LabVIEW VI by calling the following in MATLAB®:

Data = AIonechannel('2','4',2500, 20000)

This function call launches the LabVIEW user interface and starts the application. In this example, a finite acquisition on Channel 4 of Device 2 executes. The acquisition samples 2500 data points at 20 kHz. The acquired data array is returned when the function exits.

Other Options

Installing the Math Interface Toolkit creates a NI Math Interface Toolkit listing in the MATLAB® Launch Pad Toolkit category. This entry gives you access to LabVIEW (if installed), four demo examples, and a special Math Interface Toolkit product page.
[+] Enlarge Image

Conclusion

With the NI LabVIEW Math Interface Toolkit, you can easily invoke LabVIEW VIs from the MATLAB® software environment. The ability to call a LabVIEW VI from the MATLAB software environment empowers you to easily take advantage of a variety of LabVIEW features:

  • Wide-ranging I/O capabilities including plug-in data acquisition, instrument control, motion, and vision
  • Easy-to-build, customizable intuitive user interfaces
  • Communication protocols such as TCP/IP and CAN
  • More than 600 built-in LabVIEW functions for signal processing, analysis, and math

MATLAB® is a registered trademark of The MathWorks, Inc.


27 ratings | 4.07 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).