Wiki

This version (14 Jan 2021 05:46) was approved by Robin Getz.The Previously approved version (15 Jul 2020 22:17) is available.Diff

Using ADALM-2000 With MATLAB

The ADALM-2000 (M2K) can be controlled programmatically in a number of languages, including MATLAB. Controlling M2K and access the data streams is provided through a set of bindings for libm2k. If you are already familiar with libm2k, moving into MATLAB is minimal work. If you are unfamiliar, there is extensive libm2k API documentation available.

Quick Start

To get started with libm2k+MATLAB there are integrated installers available for Windows. Simply download the MLTBX file and open it within MATLAB. This will install the necessary libraries and classes to interface with M2K. The Windows installer packages the dependent libraries libiio, libm2k, and the MATLAB bindings so they do not need to be installed externally. The only requirement is the M2K driver. For Linux support see the section below.

After installation of the driver and the MLTBX file, you will have access to the bindings through MATLAB's clib interface for C++ libraries. For libm2k this will be in the form:

clib.libm2k.libm2k.<M2K classes> 
Note that not every libm2k method is supported due to MATLAB limitations but these methods are usually uncommon.

This is a common API styling MATLAB uses for external languages or even internal components that are Java-like or class-like. Below is a simple example of how to call into the library and construct an M2K object, then call into the API from that object.

%{
This example reads the analog voltage from channel 0 of the analog input
%}
%% Setup
m2k = clib.libm2k.libm2k.context.m2kOpen();
 
%% Setup analog in
ain = m2k.getAnalogIn();
ain.enableChannel(0,true);
voltage = ain.getVoltage(0);
disp(voltage);
 
%% Get more data
d3 = ain.getSamplesInterleaved(1024);
plot(d3)
 
%% Cleanup
clib.libm2k.libm2k.context.contextCloseAll();
clear m2k

More examples are available on GitHub.

Inspecting the API

The MATLAB bindings follow the C++ API. Since MATLAB has an interpreter it can be very helpful when exploring the API. This can be done through help or doc commands. For example, if you want to understand the M2kAnalogIn method getAvailableSampleRates you can query it directly.

>> help clib.libm2k.libm2k.analog.M2kAnalogIn.getAvailableSampleRates
 clib.libm2k.libm2k.analog.M2kAnalogIn/getAvailableSampleRates    Method of C++ class libm2k::analog::M2kAnalogIn.
    getAvailableSampleRates
 
    This content is from the external library documentation.
 
    Inputs
      obj            read-only clib.libm2k.libm2k.analog.M2kAnalogIn  
 
    Outputs
      RetVal         clib.array.libm2k.Double  
      The list of available samplerates for this device

This can be done through the class definitions or even created objects:

>> m2k = clib.libm2k.libm2k.context.m2kOpen()
 
m2k = 
 
  M2k with no properties.
 
>> help m2k
--- help for clib.libm2k.libm2k.context.M2k ---
 
 clib.libm2k.libm2k.context.M2k    Representation of C++ class libm2k::context::M2k.
    Contains the representation of the M2k
 
    @class M2k
    @brief Controls the ADALM2000
 
    This content is from the external library documentation.
 
    @defgroup m2k M2k
    @brief Contains the representation of the M2k
 
    @class M2k
    @brief Controls the ADALM2000
 
    Documentation for clib.libm2k.libm2k.context.M2k

Linux Support

Since libraries can vary across Linux distributions we no longer provide a packaged installer for the libm2k MATLAB bindings, but they can be easily built. They require two main dependencies which have build/install instructions:

Once these are built and installed the MATLAB bindings can be built. To do this follow these steps:

Clone the repo:

git clone https://github.com/analogdevicesinc/libm2k-matlab.git

Next, update the locations of the libm2k headers and the installed library itself in the build script. On Ubuntu these may look similar to:

    includepath  = fullfile('/usr','local','include');
    hppPath = fullfile('/usr','local','include','libm2k');
    libs = '/usr/local/lib/libm2k.so';

The library definition needs to be updated as well. On Ubuntu this will look similar to:

libDef.Libraries = '/usr/local/lib/libm2k.so';

Once those edits are complete, the entire build process can be run with the builder script. From MATLAB in the libm2k-matlab folder run:

>> builder
Using g++ compiler.
Generated definition file definelibm2k.mlx and data file 'libm2kData.xml' contain definitions for 344 constructs supported by MATLAB.
49 construct(s) require(s) additional definition. To include these construct(s) in the interface, edit the definitions in definelibm2k.mlx.
Build using build(definelibm2k).
...
...
Building interface file 'libm2kInterface.so'.
Interface file 'libm2kInterface.so' built in folder '/tmp/libm2k-matlab/libm2k'.
To use the library, add the interface file folder to the MATLAB path.

This will create a library named libm2kInterface.so in the libm2k folder. Simply adding that folder to path will allow you to use the bindings.

Support

For support questions please post them on EngineerZone under the Virtual Classroom Forum.

If you find any bugs please report them on the libm2k issues tracker on GitHub.

university/tools/m2k/matlab.txt · Last modified: 14 Jan 2021 05:38 by Robin Getz