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.
- Click on Tools >> Import >> Shared Library (.dll)… to open the Import Shared Library Wizard.
- Leave Create VIs for a shared library selected and click Next.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- In LabVIEW, open a new VI by selecting File >> New VI.
- Open your new library by navigating to it on disk or selecting File >> Open.
- Click and drag the VI you are testing onto the block diagram of your VI.
- 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.
- Compare the results of the LabVIEW VI with the results in MATLAB from test.m and note that they are the same.
Considerations
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.