Using DLLs Built with MATLAB Coder in LabVIEW

Updated Aug 31, 2023



  • LabVIEW


  • MATLAB Coder

If you use NI software to automate tests and would like to leverage analysis and algorithms written in MATLAB in those tests, one solution is to use MATLAB Coder to generate a C DLL that can be called in LabVIEW.  MATLAB Coder is an add-on toolbox for MATLAB that generates readable and portable C and C++ code from MATLAB code. MATLAB Coder supports functions from base MATLAB as well as those from other MATLAB Toolboxes.


Following this tutorial will create DLLs and wrapper VIs that can be used in 64-bit versions of NI software. If you are compiling the DLL for use in a 32-bit version of NI software, like LabVIEW 32-bit, TestStand, or VeriStand, and are on a 64-bit operating system, please follow these instructions to learn how to compile a 32-bit DLL with MATLAB Coder: Build 32-bit DLL on 64-bit Windows® Platform Using MSVC Toolchain.

This tutorial was created using LabVIEW 2020 64-bit and MATLAB and MATLAB Coder 2020a.

Build the DLL with MATLAB Coder

To generate a DLL with the MATLAB Coder add-on, follow the steps in the MathWorks tutorial: Generate C Code by Using the MATLAB Coder App and the equivalent Generate C Code at the Command Line.

  • Ensure that the Build Type is set to Dynamic Library (.dll). The files that will be used by LabVIEW are the generated DLL and the header file (.h). You can gather the required files by using the packNGo function.

Import the DLL into LabVIEW

Once you have generated the DLL using MATLAB Coder, use the Import Shared Library Wizard to create a library of LabVIEW VIs. The process is shown below with a DLL created with MATLAB Coder and the Euclidean DLL generated in the MathWorks tutorial.

  1. Click on Tools >> Import >> Shared Library (.dll)… to open the Import Shared Library Wizard.
  2. Leave Create VIs for a shared library selected and click Next.
  3. Navigate and select your generated .dll file in the Shared Library (.dll) File input. This should automatically find the .h file and put its name in the box below. If the name does not appear automatically, navigate to the .h file generated and put its path in that box, then click Next.
  1. On the Configure Include Paths and Preprocessor Definitions section, you can include any additional header files and preprocessor definitions that you need. For this example, we are using the Euclidean DLL, and including additional files is not necessary. Click Next when completed with this step.
  1. Select the functions that you would like VIs to be generated for by checking the box to the right of the function name. Select Next when complete.
    • There may be other functions that cannot be imported (indicated by an “x” to the right of the function) in the shared library, such as euclidean_initialize and euclidean_terminate as shown below. This is expected since those functions are not defined in the header file. If there is code in the initialize or terminate functions exported by MATLAB and there are prototypes for those functions in the header file, these functions will be available for import. 
  1. Input the name of your VI Library in the Project Library Name (.lvlib) input, and select the location you would like it to be saved on your system. Leaving the Copy the shared library file to the destination directory checkbox checked will create a copy of the DLL to the same directory 
  1. Choose the type of error handling you would like the library to have in it. NI suggests using Simple Error Handling to follow the normal LabVIEW programming paradigms. Click Next.
  1. The Configure VIs and Controls section is where you can configure the input and output parameters for the functions being called from the DLL. LabVIEW will automatically configure the parameters from the provided header file. Once you have compared the configuration of each VI to parameters in the header file and they match, select Next.
    1. You can select whether to pass parameters by value, pointer, or array. For any array parameters that are being passed into and out of your function, you should use Pass by Array
    2. If your MATLAB function returns multiple values, the return type of the function will be void, and there will be pointers in the function definition that correspond to the outputs. This is shown on this page as the function return parameter. For these parameters, you will set the Input/Output setting to Output so that LabVIEW knows you are not going to write any data to that parameter, only that the result of the function will be returned through that pointer.
  1. After configuring the parameters, the window will show a preview of the built library and its location. After looking over it, click Next to begin the creation of the library.
    • After showing a window that shows the progress of the build, you will be asked if you want to open the library.

You can now use the VIs from this library wherever the MATLAB functions are needed.

Test Your Application

This section will exercise the generated VIs against the test.m script referenced by the MATLAB Coder Tutorial.
  1. In MATLAB, run the test.m script as indicated in the MathWorks tutorial. Explore this script and take note of what values are being passed into the function and what the results are
  2. In LabVIEW, open a new VI by selecting File >> New VI.
  3. Open your new library by navigating to it on disk or selecting File >> Open.
  4. Click and drag the VI you are testing onto the block diagram of your VI.
  1. Create controls, indicators and constants as needed to test the functionality. Below, we use the inputs from euclidean_data.mat as used in test.m from the MATLAB Coder tutorial and receive the same outputs from the DLL. A supplementary VI was created and used to generate the data as contained in euclidean_data.mat.

  1. Compare the results of the LabVIEW VI with the results in MATLAB from test.m and note that they are the same.


In the MATLAB Coder tutorial for building the Euclidean DLL, the two-dimensional 3x216 array, cb, was converted to a one-dimensional array of length 648 automatically by the MATLAB Coder. When calling this multidimensional array in LabVIEW, we must transform this complex two-dimensional array datatype into a simpler one-dimensional array using the Reshape Array function since the Import Shared Library Wizard does not support multi-dimensional arrays.

When generating a DLL with MATLAB Coder, the default behavior is for the two-dimensional array to be built in column-major order. When reshaping an array from two-dimensional to one-dimensional, LabVIEW builds the array in row-major order. For these pieces to communicate correctly, there needs to be a change in the structure of the data. There are two options to bridge the gap between these behaviors.

MATLAB® and MATLAB Coder™ are trademarks or registered trademarks of The MathWorks, Inc.