From LabVIEW to MATLAB and Back Again:
How to Combine Both Platforms for Measurement and Data Analysis
Abstract
This tutorial provides a concise introduction to various methods of connecting National Instruments LabVIEW with MathWorks MATLAB. The aim is to demonstrate several practical approaches to integrating MATLAB scripts and functions into LabVIEW, allowing users to combine intuitive graphical programming with powerful analytical tools. All examples are illustrated on a simple calculator application.
1 Introduction
In this tutorial, we will learn how to set up communication between the NI LabVIEW and MathWorks MATLAB development environments. This will allow us to leverage the advantages of both environments when developing software tools for measurement and data acquisition. The objective is to demonstrate the fundamental principles of this communication, enabling students to incorporate it into their own programmes and projects promptly.
The examples are deliberately simple and illustrate how MATLAB code can be integrated into LabVIEW using different communication mechanisms: inline MATLAB Script nodes, external MATLAB functions, the Call MATLAB Function node, and compiled .NET assemblies.
Note:
This guide assumes a basic knowledge of development environments and programming languages.
LabVIEW is particularly well-suited to easily creating a graphical user interface (GUI) and setting up communication with measuring instruments,
primarily due to its extensive driver library (Instrument Driver Network – IDNet ) for the most commonly used instruments and controllers (e.g. Agilent, Keithley, Keysight and Tektronix).
The main advantage of these drivers is that they are available in a "plug and play" format, which makes them easy to download and copy to the root directory of the driver library:
usually C:\Program Files\National Instruments\LabVIEW yyyy\instr.lib.
On the other hand, MATLAB's text-oriented environment is better suited for scripting, where the structures of for or while loops and if conditions are significantly more compact, making variables easier to work with. Combining these two environments is ideal if the programmer has previously created MATLAB functions/scripts for data analysis and wants to add a simple user interface or integrate them directly into the measurement program.
A basic comparison of the two environments is shown in Table 1 below.
| Factor | LabVIEW | MATLAB |
|---|---|---|
| Programming paradigm | Graphical, dataflow-based “G” programming. | Text-based programming focused on numerical computing. |
| Strengths |
Excellent hardware integration; intuitive visual programming; real-time and FPGA support; natural parallelism. |
Strong mathematical and analytical toolboxes; very suitable for algorithm development; large community and support; straightforward integration with other languages. |
| Weaknesses |
High licence cost; less universal outside measurement/control; steeper learning curve for complex architectures. |
Limited, less direct hardware integration; performance limits for hard real-time; requires basic programming skills. |
| Typical use |
Instrument control and data acquisition; automated testing; sensor and actuator interfacing. |
Data analysis and simulation; algorithm design; machine learning and signal processing. |
| Costs | High licence cost and paid modules. | Also not cheap, but academic licences are available. |
| Ease of use | Quite easy for beginners thanks to the visual style. | Steeper learning curve for non-programmers. |
2 Integrating MATLAB programming into LabVIEW
We will demonstrate the integration of a MATLAB programming into LabVIEW using the example of a basic calculator. The objective is to develop a straightforward program enabling us to perform fundamental mathematical operations, such as:
- addition (\(A+B\))
- subtraction (\(A-B\))
- multiplication (\(A \cdot B\))
- division (\(\frac{A}{B}\))
- exponentiation (\(A^{B}\))
The graphical user interface (GUI), i.e. the calculator's front panel, will look the same for all VIs, as shown in Figure 1 below. To illustrate how different approaches to integration behave, we will create four VIs, programming the calculator differently in each case. For comparison, in the first case we will avoid MATLAB altogether and write the application solely in LabVIEW (Calculator_LabVIEW.vi).
2.1 Pure LabVIEW implementation
Since our calculator will enable various operations in addition to two input variables, we will use Radio Buttons to control the Case Structure and relevant mathematical operations. The resulting programme should therefore look as follows (see Figure 2).
Calculator_LabVIEW.vi).
2.2 Using the MATLAB Script node
The most straightforward way to use MATLAB syntax in LabVIEW is to use the MATLAB script block, which enables us to write MATLAB commands directly in LabVIEW. Let's now create a simple MATLAB procedure that uses an if structure to select the correct mathematical operation and return the result to variable \(C\). As is generally customary in block programming, inputs and outputs (variables) are written to the edges of the script block (inputs on the left, outputs on the right). You can add an input or output by right-clicking (Add Input/Add Output). The resulting code structure should look something like this:
if O == 0
C = 0;
elseif O == 1
C = A + B;
elseif O == 2
C = A - B;
elseif O == 3
C = A * B;
elseif O == 4
C = A / B;
else
C = A ^ B;
end
We place this code into the MATLAB Script node and bind the added inputs and outputs to the corresponding LabVIEW controls and indicators. The block diagram then should look similar to Figure 3.
Calculator_MATLAB_script.vi).
In order for this program to run properly, the full version of MATLAB must be installed. After launching the program for the first time (or reopening it), LabVIEW automatically calls a MATLAB instance, launching it in the form of a MATLAB Command Window that automatically opens. Therefore, the first launch of the program takes significantly longer time.
2.3 Calling an external MATLAB function (Calc.m)
With a MATLAB script, we can also directly call predefined MATLAB functions. However, either the MATLAB instance must be run in the root directory of the function or the function must be added to the path so that MATLAB can access it from anywhere. Therefore, we can define the above if structure as a separate function, Calc(A, B, O), in a file named Calc.m, which we will then add to the path and call it directly from LabVIEW.
function C = Calc(A, B, O)
if O == 0
C = 0;
elseif O == 1
C = A + B;
elseif O == 2
C = A - B;
elseif O == 3
C = A * B;
elseif O == 4
C = A / B;
else
C = A ^ B;
end
end
In LabVIEW, the MATLAB Script node can now be simplified to just two lines. The first line will call the addpath command,which will allow us to add the folder containing the Calc.m function to the path. While the second line will contain the function call itself (Figure 4).
addpath('C:\path\to\Calc\folder'); % adjust the path as needed
C = Calc(A, B, O);
Calc.m function (Calculator_MATLAB_script_2.vi).
3 Using the Call MATLAB Function node
The next, more advanced option is the Call MATLAB Function node, which is available in the palette Connectivity → MATLAB(R) and was introduced in LabVIEW 2021. This node is designed to work together with Open MATLAB Session and Close MATLAB Session nodes, which take care of starting and closing the MATLAB instance. In this setup, the MATLAB engine runs in the background only – the user does not see a separate command window.
To use this node correctly, we must specify the path to the MATLAB function we want to call (once again, Calc.m). This can be given either as an absolute path or as a relative path, for example if the function is stored right next to the VI in the working directory.
A subtle but important detail is the syntax for inputs and outputs. In addition to the input arguments of the MATLAB function (here, \(A\), \(B\) and \(O\)), we must connect a 'variable example' - either a Constant or a Cluster that describes all expected outputs.
For the calculator, a single DBL constant is sufficient (Figure 1). If the MATLAB function returns multiple outputs, we need to define a cluster containing all required types.
Call MATLAB Function node and Calc.m
(Calculator_MATLAB_Function.vi).
Figure 2 shows an example where the MATLAB function has several outputs: two numeric arrays and one Boolean. In that case we construct a corresponding cluster as the output template and then recover the individual elements using Unbundle or Unbundle By Name nodes.
The main advantage of this communication style is that you control exactly when the MATLAB session is opened and closed, as well as handle an error that may occure. In larger applications you can, for example, initialise the MATLAB engine in a dedicated “Init” state of a Simple State Machine and close it in a “Shutdown” state, together with other resources.
4 Integrating a compiled MATLAB function via .NET assambly
The most advanced – and also most complex – option is to use the .NET platform. Compared to the previous approaches, this one requires more setup, but offers one key advantage: the resulting VI, or even a compiled executable built .exe application from it, no longer needs a full MATLAB installation. It only requires the appropriate MATLAB Runtime.
The workflow is as follows: we first compile the MATLAB function using the MATLAB Compiler and MATLAB Compiler SDK toolboxes into a .NET assembly .dll. We then call this assembly from LabVIEW using the .NET palette.
You can check whether the necessary toolboxes are installed in MATLAB by executing:
>> ver
MATLAB prints the version and a list of installed components. If MATLAB Compiler or MATLAB Compiler SDK are not present, they can be added by the Get Add-Ons button (HOME → ENVIRONMENT → Get Add-Ons).
4.1 Compiling Calc.m into a .NET assembly
Returning to our Calc.m function, we now compile it into a .NET assembly using the following command:
>> mcc -W 'dotnet:CalcLib,CalcClass' -T link:lib Calc.m -d .\NET
Where the arguments have the following meaning:
- -W 'dotnet:CalcLib,CalcClass' – creates a .NET assembly CalcLib.dll with a class named CalcClass.
- -T link:lib – compiles the MATLAB function as a shared .NET library (DLL).
- Calc.m – the MATLAB function to be compiled.
- -d .\NET – stores all generated files in the subfolder NET\ of the current directory.
The result is a NET directory containing the compiled .dll files and several auxiliary files, as illustrated in Figure 1.
NET folder after
compiling Calc.m.
At this point, MATLAB itself is no longer needed for runtime use and we can switch back to LabVIEW.
4.2 Creating the LabVIEW VI using .NET
In LabVIEW we proceed in several steps:
-
Create a Constructor Node for the .NET assembly.
(Functions Palette → Connectivity → .NET → Constructor Node)
When the Constructor Node is placed on the block diagram, a dialog window appears. There we have to browse to CalcLib.dll (or MyCalcLib.dll, depending on the name used in mcc) and select the appropriate constructor - CalcClass(), which gives us a reference to the .NET object. -
Add an Invoke Node and wire it to the .NET reference.
(Connectivity → .NET → Invoke Node)
Here we need to select appropriate method from the method list. Select: Calc(Int32 numArgsOut, MWArray A, MWArray B, MWArray O).
This method allows us to pass the three input variables \(A\), \(B\) and \(O\) to our Calc function.The intermediate block diagram shoold look approximately as in Figure 2.
Creating the .NET reference and selecting the Calc()method. - Define the inputs and outputs.
As in the previous case, we must now define the inputs and outputs expected by the Calc() function. For the outputs we simply specify how many there are – here we set numArgsOut = 1 by wiring a constant 1.However the inputs \(A\), \(B\) and \(O\) are more subtle because MATLAB uses its own object type MWArray. To pass LabVIEW data to the compiled .NET function, we must make use of the MWArray.dll library, which is typically located at:
<mcr_root>\toolbox\dotnetbuilder\bin\win64\v4.0We add another Constructor Node, this time for the MWArray assembly, and select the MWNumericArray class. Then we choose an appropriate constructor, such as MWNumericArray(Double scalar), and wire the corresponding LabVIEW DBL values into it. The resulting MWNumericArray objects are connected to the \(A\), \(B\) and \(O\) inputs of Calc() function, Figure 3.
Converting LabVIEW numeric values to MATLAB MWArrayobjects usingMWNumericArray. - Convert outputs back to LabVIEW variables.
Similarly, the outputs of Calc() must be converted back from MWArray to LabVIEW data types. Even if Calc() returns a single scalar, the generic MATLAB output is an array, here conceptually [C]. Thus, we first have to use Index Array to extract the first element, which is still an MWArray object.We then use To More Specific Class node (from Connectivity → .NET), to cast this object to MWNumericArray, for which we already have a reference from the input conversion. Finally, we call the method ToScalarDouble() to obtain a plain LabVIEW DBL type variable.
The final block diagram of the calculator using the compiled .NET library is shown schematically in Figure 4.
Final block diagram of the calculator based on a compiled MATLAB .NET library ( Calculator_.NET.vi).
A VI built in this way does not require a full MATLAB installation at runtime – only the appropriate MATLAB Runtime version that matches the version used for compilation.
This approach allows you to build VIs that can later be turned into standalone applications running on machines without MATLAB licences. It is therefore a convenient way of integrating MATLAB functions that you have developed earlier, provided that you compile them while you still have access to the necessary toolboxes and licences.
5 Conclusion
In this short tutorial we have explored several ways of integrating MATLAB scripts and functions into LabVIEW, using a simple calculator example as a guiding case.
Each environment has its own quirks and design philosophy. Even a very elegant integration of two platforms will not magically remove these differences – in fact, it may introduce new layers of complexity. The goal of an experienced developer should therefore remain to keep things as simple as reasonably possible.
At the same time, there are clear situations where combining LabVIEW and MATLAB is both useful and efficient: when you already have well-tested MATLAB analysis code and need a measurement GUI, when you want to build a data acquisition system around existing scripts, or when you plan to deploy compiled .NET libraries without full MATLAB installations.
Note:
If you have any questions or experience any communication issues that are not covered by this guide,
please refer to the extensive LabVIEW and Matlab communities , .