Migrating to LabVIEW MATLAB Functions from MathScript Nodes

Updated Sep 5, 2023



  • LabVIEW 2021

Operating System

  • Windows


  • MATLAB® 

LabVIEW MATLAB®  functions are recommended as an alternative to LabVIEW MathScript Nodes for Windows computers. NI does not recommend LabVIEW MathScript Node for new designs as this structure was deprecated in LabVIEW 2023 Q3. 

This tutorial walks through how to migrate code that uses MathScript nodes to MATLAB Functions. 

Software Considerations:

  • You must have a licensed copy of the MATLAB® software version 6.5 or later installed to use LabVIEW MATLAB Functions.
  • LabVIEW MATLAB Functions are available only on Windows OS. 
  • LabVIEW version required: 2021 and above

To migrate VIs containing MathScript nodes to their equivalent MATLAB session functions, we need to convert the MathScript function to an equivalent .m function.
  1. Open MATLAB and create a new .m file
  2. Open your LabVIEW code that contains MathScript Nodes
  3. Identify the MathScript functions used in your code.   
    1. (Optional) For an easy test, you can copy the functions from the script node in your .m file and try to run.
      1. If there are no errors and you are seeing expected results, then you may not need to update your script and can move forward to step 5.
      2. If there are errors, continue to the next step to determine which functions will need to be replaced and what to migrate them to.
  4. Determine what functions from your code need to be migrated, and what MATLAB functions they need to be replaced with.
    1. Some basic MathScript functions (like plot, sin, cos, etc.) are the same in MATLAB and do not require changing the function in your script.
      1. See the Simple Migration example below
    2. Many MathScript functions have an analogous MATLAB function. Check whether your MathScript functions have corresponding function names in MathWorks products.
      1. See the Complex Migration example below
    3. If corresponding functions are not found in the migration table, you can check the MATLAB community’s File Exchange page to see if suitable alternatives are available.
      1. See MathScript Code Without Direct Mappings example below
  5. Create the function(s) in the .m file that replicate the purpose of your code utilizing the corresponding code you found in the previous step.
  6. Save the .m file, noting its name and file path.
  7. In LabVIEW, call the .m file by opening a MATLAB session.
    1. Use the MATALB functions in LabVIEW to create and configure your session: Open MATLAB Session, Call MATLAB, and Close MATLAB Session.
    2. You must specify the path of the .m file along with any other input parameters to the actual function itself.
    3. Set up your code as shown in the snippet below
MATLAB functions calling a .m file in LabVIEW
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram. 



Datatype support
Numerical datatypes do not automatically convert between MATLAB and LabVIEW during interop. Meaning, if LabVIEW expects an array of doubles as return type but sees an array of float, it will throw an error citing mismatch in datatypes. Datatype comparison is strict between the boundaries.

Simple Migration Example

For most cases the conversion is straightforward. For example, consider the following MathScript function which plots sine and cosine waves.
This script in the MathScript node would look like:
t = 0:0.1:2*pi;
plot(t, sin(t), t, cos(t));

To convert this into MATLAB function and call it from LabVIEW:
  1. Create a .m file (say customPlot.m) and
  2. Create a function matching the name of the file, and copy the contents from the MathScript node as the function body:
function customPlot()    
t = 0:0.1:2*pi;
plot(t, sin(t), t, cos(t));
  1. Call this function from LabVIEW by opening a MATLAB session. This is done via the Open MATLAB Session function.
  2. The session refnum is then passed into Call MATLAB function which is responsible for calling into .m functions.
  3.  You must specify the path of the .m file as input to the node along with any other input parameters to the actual function itself.
  4.  Finally, you close the session with a Close MATLAB Session function.

Diagram  Description automatically generated

Complex Migration Example

Not all MathScript function names map directly to corresponding MATLAB function names. Consider the conversion of a more complex example.

The example below utilizes MathScript functions from the MathScript RT module. If you have the module, you can find the file in <LabVIEW Directory>\examples\MathScript\MathScript Fractal.vi

The MathScript Node for generating Mandelbrot Set looks like this:
Timeline  Description automatically generated

The functions timerstart, timerstop, linramp and meshgrid2d are replaced with the corresponding MathWorks functions. The resulting MATLAB function would look like this:
function [xmult,ymult,time, W] = MandlebrotSet(xmax, xmin, ymax, ymin)
t = tic;
xmult = (xmax-xmin)/m;
ymult = (ymax-ymin)/m;
for k=1:maxiter

The first line indicates the function name; the input parameters are in the parenthesis after the function name and the outputs in the closing brackets. Here, we see that timerstart and timerstop are replaced by tic and toc; linramp by linespace and meshgrid2d by meshgrid.

When a MATLAB function, which returns multiple elements, is called from LabVIEW, we use a cluster with the same number of elements (as return types), for the return type. The types in the cluster should be ordered and each element should have the same name as the corresponding return type in the MATLAB function.
Diagram  Description automatically generated
Note: Code above is from LabVIEW example <LabVIEW 2021 Install Dir>\examples\Mathematics\Scripts and Formulas\MATLAB node - Lorenz Diff Eq.vi


MathScript Code Without Direct Mappings

Some MathScript functions may not have a direct mapping to MATLAB, but alternative for those can usually be found.

MathScript-Specific Functions

For instance, MathScript node will have access to more built-in functions and datatypes if you have LabVIEW Control Design and Simulation (CD & Sim) module installed.
One such example is the function ackermann. With CD & Sim installed, this function can readily be invoked in LabVIEW MathScript code as a built-in function. In base MATLAB there is no built-in equivalent, but through the community’s File Exchange page, you can find suitable alternatives like this one.

MATLAB-Specific Functions

For some built-in functions you would also need to install additional MathWorks products on top of MATLAB.
For example, the ss function is not present in the base MATLAB and would require additional addons to be installed. Using the function in MATLAB console will prompt the missing addon which is required for the function execution.

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