MATLAB for UD - Windows | LabJack
« Close

Datasheets and User Guides

App Notes

Software & Driver


MATLAB for UD - Windows

MATLAB examples for the Windows UD library. The Windows UD library is the high-level Windows library/driver for the LabJack U3, U6 and UE9. For other LabJack devices, go to the Software page.

Warning: If you intend to create a 64-bit application please ensure you use AddRequestPtr rather than AddRequest anywhere you write or read an array of data. See our AddRequest documentation for more information.


MATLAB .NET examples for R2009a (7.8) and later

The recommended examples for modern MATLAB. Requires MATLAB R2009a (7.8) and later. The ".NET Examples for MATLAB" attachment below provides examples that use the MATLAB .NET interface and the UD .NET assembly. No compiler tools are necessary. Refer to the README.txt in the download for details. Download dated 04/25/2018 are the latest.

Note to MATLAB 2018a and newer users: If you have script code using a LabJack that was working in previous MATLAB versions but is now getting "Undefined variable" errors when using enumerations, refer to the latest download's README.txt file, section "Changes for MATLAB 2018a and Newer", for solutions.


Old MATLAB examples for R2008b (7.7) and earlier

The old MATLAB drivers and examples version 1.32. See the PDF included in the download zip. Tested on MATLAB 7.0, but should work with 6.5 or newer.

This should work for most 32-bit versions of MATLAB. Due to how MATLAB handles calls to .dlls, additional steps are needed for work with 64-bit MATLAB. In addition to needing the 64-bit LabJackUD.dll you must also have a set of compiler tools to call the functions.  

See this for an example of how to set that up with MATLAB 7.7

For MATLAB R2010a you can get more information here for 32-bit versions and here for 64-bit versions.

For MATLAB R2011b you can get more information here for 32-bit versions and here for 64-bit versions.

For MATLAB R2012b you can get more information here for 32-bit versions and here for 64-bit versions.

For other versions please see the "Supported Compilers" column on this page.


UD Examples Overview (Referencable)

UD Library Overview

First read the general overview of the UD library:

The UD driver has a handful of functions, many with the same 4 to 6 parameters.


The main functions are used to build a list of requests (Add), execute the list (Go), and read the result of each request (Get).  Some functions (e.g. eGet) combine a single add/go/get into one function call.


The parameters are described at the bottom of the Overview page.  The meaning of each parameter should be apparent (e.g. Channel is the channel number you are operating on), but when it is not the pseudocode section of the device's datasheet has the extra information needed.

  • Handle:  Always the handle.
  • IOType:  Always the IOType.
  • Channel:  If something besides a channel number the pseudocode section will tell you.
  • Value:  Always the value.
  • X1:  Usually not used, but if used the pseudocode section will tell you.
  • UserData:  Usually not used, but if used the pseudocode section will tell you.


Device specific pseudocode can be found in datasheet section 4.3:



I noticed in the U3_Simple_Logger.m file that you don't preinitialize the size of the array for Error, AINX, and FIOX.  This will dramatically decrease the speed of data acquisition as the array gets larger.  Since you are already requesting the 'time' and 'dt' variables, the following lines should be added at the beginning of the program to increase program speed for longer tests.

  • Error(1,1:time/dt)=0;
  • AIN0(1,1:time/dt)=-100;
  • AIN1(1,1:time/dt)=-100;
  • AIN2(1,1:time/dt)=-100;
  • AIN3(1,1:time/dt)=-100;
  • FIO0(1,1:time/dt)=-100;
  • FIO1(1,1:time/dt)=-100;

It may be desirable to initialize values to -100 in case samples are missed (these values could get filtered later).

- A concerned citizen with Matlab expertise


Thank you for your suggestion and we will be sure to fix that in the examples as soon as possible.


-- Labjack Support


Could you please help me with Labjack U6 access in Matlab Linux?

I found this tread, which is about Matlab and Mac, which only creates access

through the lower part of the driver (exodriver):

It is pretty helpfull, but i also want the higher level functions like in the u6.c.

For this i compiled the u6.c to a library (.so) and loaded that one into Matlab.

Then i am able to e.g. change digital 0 out from low to high with the following Matlab call:

calllib( 'libu6', 'eDO', handle, 0, 1 );

or read the state of digital 1 input with:

calllib( 'libu6', 'eDI', handle, 1, state );

However if i want to use the analog stuf, but then i need to get the calibration values first.


calibrationinfo.prodID = 0;


calibrationinfo.hiRes = 0;

calibrationinfo.ccConstants = zeros(1,40);

if( calllib( 'libu6', 'getCalibrationInfo', handle, calibrationinfo ) ~= 0 )

   error( 'problem with getting calibrationinfo' );


But whatever i try the cells in the calibrationinfo struct stay empty.


So there is something going wrong with passing the calibrationinfo struct

back to the Matlab environment.

Can you help me out?





Try using libstruct to create the struct and see if that helps.  Here's the libstruct and calllib documentation (provides libstruct examples):

Hi thanks for your answer,

I could pass an empty array as input, and get the calibration struct

from one of the return values.

See example .m code below:

% Get calibration information, required for the ADC and DAC:


[state, tmp, cali] = calllib( 'libu6', 'getCalibrationInfo', handle, [] );

% Get ADC value from AIN0:

[state, tmp, tmp2, value] = calllib( 'libu6', 'eAIN', handle, cali, 0, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

Except for a bunch of warnings when loading the u6 library in Matlab, everything

works amazingly.

Thanks again,



Hi all,

Here an example Matlab script for linux using a compiled version of the u6.c as shared library to get access to the high level functions like eAIN and eDAC.

Have fun




% Andre, March 27, 2012

% Version 1.2

% Tested on 32 bit Ubuntu 10.10 Linux

% Tested with MatLab (R2010a)


% Use the higher level u6 functions, like eAIN and eDAC, with ONE LabJack U6.


% Loading the u6 library generates quite some warnings, but it looks like

% everything works just fine.


clear all;



my_reload = 0;

if my_reload ~= 0

   disp( 'unload libraries' );





% Load exodriver, which contains low level functions like read and write

if ~ libisloaded('liblabjackusb')

   disp( 'load labjack library' );


   header = '/usr/local/include/labjackusb.h';

   ljlib = '/usr/local/lib/';

   [notfound,warnings]=loadlibrary(ljlib, header);



% Load u6 library, which contains high level functions like eAIN and eDAC

if ~ libisloaded('libu6')

   disp( 'load u6 library' );


   % Created shared libary with the following command:

   % gcc -Wall -g -fPIC -shared /usr/local/lib/ -Wl,-soname, -o u6.c

   u6header = '/home/andre/matlab/u6.h';

   u6ljlib = '/home/andre/matlab/';

   [notfound,warnings]=loadlibrary(u6ljlib, u6header);

   % Andre: this loadlibary gives a lot of WARNINGS!!

   % disp( warnings );



% Show available functions with arguments of both libraries:

% libfunctionsview liblabjackusb

% libfunctionsview libu6


% Check LabJack library version:

if calllib( 'liblabjackusb', 'LJUSB_GetLibraryVersion' ) ~= 2.0

   warning( 'This implementation is only tested with library version 2.0' );



% Check if there is exactly ONE LabJack attached:

if calllib( 'liblabjackusb', 'LJUSB_GetDevCount', LJ_dtU6 ) ~= 1

   error( 'There should be exactly ONE LabJack U6 device attached' );



% Open the device and verify the returned handle:

handle = calllib( 'liblabjackusb', 'LJUSB_OpenDevice', 1, 0, LJ_dtU6 );

[handle_valid, tmp] = calllib( 'liblabjackusb', 'LJUSB_IsHandleValid', handle );

if ~handle_valid error( 'LabJack handle is invalid' ); end


% Get calibration information, required for the ADC and DAC:

[state, tmp, cali] = calllib( 'libu6', 'getCalibrationInfo', handle, [] );

if state ~= 0 error( 'Cannot get calibration info' ); end


% Check if the calibration struct contains appropriate data:

if cali.prodID ~= LJ_dtU6 error( 'Calibration struct is invalid' ); end


% Get ADC value from AIN0 to AIN14 and Labjack temperature:

for chan=1:15,

   [state, tmp, tmp2, value(chan)] = calllib( 'libu6', 'eAIN', handle, cali, chan-1, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

   if state ~= 0 error( 'Problem getting eAIN' ); end



fprintf( 'LabJack temperature: %.2fC\n', value(15)-273.15 );


fprintf( 'ADC val:  ' );

for chan=1:14,

   fprintf( ' %4.2f', value(chan) );


fprintf( '\n' );


% Now determine the mean, maximum and minimum for each channel for a number of samples

measure = zeros( 100, 6 ); % number of samples, number of channels

fprintf( 'Collect %d samples for %d ADC inputs (interleaved)\n', size(measure,1), size(measure,2) );

for ii = 1:size(measure,1),

   for chan = 1:size(measure,2),

      [state, tmp, tmp2, measure(ii,chan)] = calllib( 'libu6', 'eAIN', handle, cali, chan-1, 15, 0, LJ_rgBIP10V, 0, 0, 0, 0, 0 );

      if state ~= 0 error( 'Problem getting eAIN' ); end




fprintf( 'ADC mean: ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', mean(measure( :, chan) ) );


fprintf( '\n' );


fprintf( 'ADC max:  ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', max(measure( :, chan) ) );


fprintf( '\n' );


fprintf( 'ADC min:  ' );

for chan = 1:size(measure,2),

   fprintf( ' %4.2f', min(measure( :, chan) ) );


fprintf( '\n' );



title( '\bf{\it{LabJack U6 ADC Measurement}}' );


legend_str = {};

for chan = 1:size(measure,2),

   legend_str( chan ) = {['AIN ', num2str( chan ) ]};



legend( legend_str );

xlabel( 'Time [samples]' );

ylabel( 'Amplitude [V]' );

grid on;


% Test the DAC by setting 2.55V on DAC 0 and 1.80V on DAC 1

[state, tmp, tmp2] = calllib( 'libu6', 'eDAC', handle, cali, 0, 2.55, 0, 0, 0 );

if state ~= 0 error( 'Problem setting eDAC' ); end

[state, tmp, tmp2] = calllib( 'libu6', 'eDAC', handle, cali, 1, 1.80, 0, 0, 0 );

if state ~= 0 error( 'Problem setting eDAC' ); end


% Digital out FIO, set FIO 0 to high, set FIO 2 to low

[state, tmp] = calllib( 'libu6', 'eDO', handle, 0, 1 );

if state ~= 0 error( 'Problem getting eD0' ); end


[state, tmp] = calllib( 'libu6', 'eDO', handle, 2, 0 );

if state ~= 0 error( 'Problem getting eD0' ); end


% Digital in FIO, get FIO 1 (should be high) and FIO 3 (should be low)

% Asume FIO 0 is connected to FIO 1 and FIO 2 is connected with FIO 3

[state, tmp, fio1] = calllib( 'libu6', 'eDI', handle, 1, 0 );

if state ~= 0 error( 'Problem getting eDI' ); end

if fio1 ~= 1 warning( 'Should be high if connected to FIO 0' ); end


[state, tmp, fio3] = calllib( 'libu6', 'eDI', handle, 3, 0 );

if state ~= 0 error( 'Problem with getting eDI' ); end

if fio3 ~= 0 warning( 'Should be high if connected to FIO 2' ); end


% All done, close LabJack:

calllib( 'liblabjackusb', 'LJUSB_CloseDevice', handle );


fprintf( 'All done!\n' );


when I wanted to load the standard LabJackUD library into MATLAB using ljud_LoadDriver function it showed error like this.

??? Error using ==> loadlibrary>lFullPath at 550
Could not find file C:\progra~1\LabJack\drivers\labjackud.h.

Error in ==> loadlibrary at 220

Error in ==> ljud_LoadDriver at 10

I checked the required path and header file is still there.Can u give a solution for this?

Sometimes this is caused by 64-bit systems, since the file will be in C:\program files(x86)\LabJack\drivers instead of C:\Program files\LabJack\Drivers.

Other things you can try are putting the header file into an easier to find spot C:\header\ or something similar then changing the path links.  Sometimes it's a problem with folder names being more than 8 characters.


We have tried this, but we now get 'Index exeeds matrix dimensions.'



Make sure you have the required C compiler installed for loadlibrary and related calls to work in your version of MATLAB:  (supported compilers for different versions of MATLAB)

I've seen that error occur when the C compiler is not installed.


i'm using a U3HV,

when i try to load drivers:

>> ljud_LoadDriver
Error using loadlibrary (line 419)
Building labjackud_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\Program Files\MATLAB\R2012a\extern\include" /W3 /D_CRT_SECURE_NO_DEPRECATE
-I"\\\mb5912\MATLAB\Cylinder Simulation\MATLAB_LJUD\LJUD_Functions"
-I"C:\" "labjackud_thunk_pcwin64.c" -LD -Fe"labjackud_thunk_pcwin64.dll"
cl : Command line error D8003 : missing source filename

Error in ljud_LoadDriver (line 10)

What can i do?

Many thank's!

That isn't one we have seen before.  Is it a 64-bit version of MATLAB?  You might try using the 32-bit version (which tends to have less of these kinds of errors) or install our 64-bit version of the LabJackUD.dll which can be found here:

**Edit -  This separate .dll is no longer needed since the current LabJack installer includes everything for both 32-bit and 64-bit versions.

Hi, I'm using the provided .NET Matlab examples on a U6 with the latest driver and firmware. I'm having show show-stopping problems running u6_simplestream.m:

1. I'm getting "Stream scan overlapped" error from Matlab with the same configuration as LJStreamUD. In fact, Matlab could only support ~1/2 the maximum rate LJStreamUD supports.

2. Under 64bit win7 with 64bit Matlab, I'm getting a System.AccessViolationException when at the ljudObj.eGet() that attempts to read the stream data. The full Matlab output is as follows:

UD Driver Version = 3.39
Actual Scan Rate = 100
Actual Sample Rate = 200

.NET Error: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at LabJack.LabJackUD.LJUD.UDeGet(Int32 handle, IO IOType, CHANNEL channel, Double& val, Double[] x1)
   at LabJack.LabJackUD.LJUD.eGet(Int32 handle, IO IOType, CHANNEL channel, Double& val, Double[] x1)
Error using u6_simplestream (line 125)

Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source: LJUDDotNet

Help anybody?

I responded to this question in your forum topic here:


I am working with a LabJack U3 for Matlab on Windows

I had a problem with a command line, the statement of chanType


The error is "Get Type is not a method, property or field to CHANNEL class"

I need help with this. Any suggestion?


It looks like you are using the .NET examples with MATLAB 2010 or older. We've seen issues where older MATLAB versions can't handle these type of .NET calls, giving the error you are seeing, but the MATLAB version we tested with (2013a) runs the code fine.

First, make sure you are running the latest version of the Windows software for the U3:

Second, remove the line of code causing the issue and the "System.Enum.ToObject" calls that use the chanType object. That code is used to typecast a value to a LabJack.LabJackUD.CHANNEL object but doesn't work in MATLAB 2010 or older. Since the .NET examples' release, we have updated the UD .NET driver where these typecasts are not necessary and you can pass just the channel value. For example, instead of:

    chanObj = System.Enum.ToObject(chanType, 0); %channel = 0
    ljudObj.ePut(ljhandle, LabJack.LabJackUD.IO.PIN_CONFIGURATION_RESET, chanObj, 0, 0);


    ljudObj.ePut(ljhandle, LabJack.LabJackUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);

Sometime we will look into updating the examples to work right away under MATLAB 2009 to 2010 versions, but in the meantime the above code is a workaround for these issues.

To update this issue, the examples have been updated. The "Get Type is not a method, property or field to CHANNEL class" and similar errors have been resolved when using MATLAB 2009 to 2010.


I am using LabJack U3-HV on Mac OS X. I downloaded the exodriver, but unsure how to use it from MATLAB to read trigger signals.

I can load the liblabjackusb into Matlab, but do not know which functions (e.g. there are multiple read functions) and what input arguments to use.

Could you help me? Any example code would be also appreciated.


We do not provide MATLAB examples for Mac OS X. To help with Exodriver function usage with a U3, take a look at the C examples included in the Exodriver download:

Also, the Exodriver functions' documentation are in the labjackusb.h header file.

The user Iandol on our forum made a U3/U6 MATLAB class for Mac OS X which may be helpful:

It´s Posible to use the labJack on Simulink. I try  it but I can´t. 

We do not provide a Simulink specific interface, but you can use MATLAB code to use a LabJack in Simulink. The examples only demonstrate LabJack usage in MATLAB. If you are having MATLAB code issues, please describe the problem you are having and/or provide error messages.

I'm trying to run the u3_simple emample in Matlab, but getting the following error:

   UD Driver Version = 3.43
   UD Error: Invalid channel number
   Error using u3_simple (line 30)
   Message: Error in the application.
   Source: LJUDDotNet

I'm using a U3-HV. I have the latest firmware and drivers. I have the 32-bit R2013a Student Version of Matlab on a 64-bit Windows 7 OS.

The u3_simplestream is giving the same error message (line 53), but the u3_timer_counter example is working.

We are currently trying to get access to a copy of MATLAB 2013b for testing. Another user has the same issue in 2013b as well. The current examples were tested in MATLAB R2009b and didn't have issues.

Looks like the issue was related to this U3 call:

ljudObj.ePut(ljhandle, LabJack.LabJackUD.IO.PUT_ANALOG_ENABLE_PORT, 0, 3, 16);

We updated the examples so these calls will work in newer versions of MATLAB. Also, we fixed the stream example for 64-bit versions of MATLAB.

Let us know if you run into further problems.

I am using Matlab 2013b 64 bit version on a WIndows 8.1 OS. I am trying to get the Labjack U6 examples to run and I keep getting the error


UD Driver Version = 3.37

UD Error: Stream is running 

Error using u6_simple (line 70)

Message: Error in the application.

Source: LJUDDotNet



Can you guide me on this?



Hi, I'm new whit the U3,  I am using Matlab and I was trying to sample two analog inputs whith a sample frequency of 20000 (Samp/sec) but I am not able.

the error is:  Buffer overrun while streaming...

I'm usign LJ_swSLEEP mode.  Could some body help me please...?

When streaming both the UD driver and the U3 buffer samples which your program reads from. If your program does not read at a fast enough rate (in your case 20000 samples/sec) these buffers will overflow leading to the error you are seeing.

If using LJ_swSLEEP mode, remove the "pause" from your read stream data loop as it is not needed and the UD driver delays until the amount of scans you specify are available. Also, make sure your stream buffer size is large enough for your scans rate (LJ_chSTREAM_BUFFER_SIZE) which we recommend configuring to 5 seconds worth of scans (scanRate * # channels * 5 seconds). For streaming using the UD driver, refer to here in the User's Guide:

If that doesn't help, please email your code to us at [email protected] so we can see how you configured stream mode and are reading data.

Hi, I am using the U3 with Matlab 2013a on windows and for some reason I can't get a simple LED linked to the DAC0 to flicker at 10z.

Is it an issue with my line opening the labjack, adding the requests or executing the requests or is it that you cannot use the pause function?

Here is my code, hope you can help me ! :

%Open the first found LabJack U3.

[ljerror, ljhandle] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '0', true, 0);

prompt = 'Length of flicker ? ';

s = input(prompt);

for n = 1:s

%AddRequest (Handle, IOType, Channel, Value, x1, UserData)

%Request that DAC0 be set to 4 volts.

 ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_DAC, 0, 4.0, 0, 0);

pause(0.05) % in seconds


% then to 0 volts

 ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_DAC, 0, 0.00, 0, 0);



%Execute the requests.


It looks like you are building a list of DAC0 requests, and are then sending all those requests to the U3 outside the loop so your pause delay will have no effect. You can use pause for the delays, but you need to use the GoOne call after the AddRequest to set the DAC with your loop timing. Try something like this instead in your loop:

%Request that DAC0 be set to 4 volts.
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_DAC, 0, 4.0, 0, 0);
ljudObj.GoOne(ljhandle); %Send the request

pause(0.05) %wait 0.05 seconds

% then to 0 volts
ljudObj.AddRequest(ljhandle, LabJack.LabJackUD.IO.PUT_DAC, 0, 0.00, 0, 0);
ljudObj.GoOne(ljhandle); %Send the request
pause(0.05) %wait 0.05 seconds

For a general UD driver overview where AddRequest, Go/GoOne, GetResult are discussed, look here: