This tutorial utilizes The MathWorks, Inc. MATLAB®, MATLAB Compiler™, and MATLAB Compiler SDK™ to create a .NET assembly that can be called in LabVIEW. This approach greatly reduces overhead and improves runtime compared to other methods such as calling executables or MATLAB script node.
Integrating MATLAB to LabVIEW enables complete reuse of MATLAB code. At the end of the tutorial, scientists and engineers who use both platforms should be able to seamlessly integrate MATLAB in LabVIEW.
LabVIEW Development System
MATLAB, MATLAB Compiler™, and MATLAB Compiler SDK™
On the deployment machines you need to install MATLAB Runtime
Note: The version of MATLAB Runtime should be the same as the version of MATLAB that was used to create the compiled MATLAB code.
Generate a .NET assembly from MATLAB using the Library Compiler App. The tutorial from MathWorks, Inc. uses the makesquare.m example file and results in the creating of MagicSquareCompNative.dll, which is the file we will be using to integrate into LabVIEW in this tutorial.
Verify the generated folders and files. Three folders are generated in the target folder location: for_redistribution, for_redistribution_files_only, and for_testing. We will be using MagicSquareCompNative.dll in for_testing folder for the next step. Note: We use MagicSquareCompNative.dll rather than MagicSqaureComp.dll because MagicSqaureComp.dll uses M array data type and this data type is not supported in LabVIEW.
Open a new VI by launching LabVIEW >> File >> New VI.
On the block diagram, add a Constructor Node from the Connectivity >> .NET pallet.
When you drop the Constructor Node on the block diagram, the Select .NET Constructor configuration window will pop up. In the Assembly drop-down , select MagicSquareCompNative.dll from the built package folder >> Class1 in Objects >> OK.
From the Connectivity >> .NET pallet, add an Invoke Node(.NET) to the block diagram. Invoke Node invokes a method or action on a reference.
Wire the output reference of the Constructor Node to the input reference of the Invoke Node. From the dropdown of the Invoke Node, select makesquare(Int32 numArgsOut, Object x) function.
“numArgsOut” is the number of output arguments to return. Since makesquare function only returns one output, set this input to a constant of 1.
Input “x” is a .NET object. Use the Connectivity >> .NET >> To .NET Object.vi to convert input data type to a .NET object.
Add a numeric array to your front panel. On the block diagram, wire the array control into the To .NET Object input.
Output “makesquare” is a .NET object that needs to convert to a variant. Use Connectivity >> .NET >>.NET Object To Variant.vi and Cluster, Class, and Variant >>Variant >> Variant to Data.VI. Configure the Variant to Data.VI to output a numeric result.
Close .NET Reference with the Connectivity >> .NET >> Close Reference.VI.
The Front Panel should look like the following VI:
Test this application by assign input array of value 5. Below is the result.
Compare this result with the result from MATLAB’s makesquare.m.
Note on benchmark testing: In benchmark testing using the tic toc method, the MATLAB .NET library call was 1,783% faster than running the same code in LabVIEW MathScript Node and 16% faster compared to bundling the MATLAB code as an EXE and calling in with the LabVIEW exec.vi. The execution time for testing depends many factors such as the code being executed and computer's processing power. You can benchmark your own code by following the steps in Benchmarking Sections of Code in LabVIEW.
Generate a .NET Assembly and Build a .NET Application
.NET Functions – LabVIEW Help
Other information on interfacing MATLAB and LabVIEW
Interfacing LabVIEW Communications to MATLAB® Software
Calling the MATLAB® Software from LabVIEW – LabVIEW Help
Interfaces for MATLAB – LabVIEW NXG Manual
MATLAB®, MATLAB Compiler™, and MATLAB Compiler SDK™ are trademarks or registered trademarks of The MathWorks, Inc.
Collaborate with other users in our discussion forums
A valid service agreement may be required, and support options vary by country.