Matlab Programming
The MathWorks' numerical computing environment
Matlab® has powerful tools for data analysis
and visualization that can be used to create graphical user interfaces
or automatically generate reports of experimental results in various
formats. LabOne’s Matlab API, also known as ziDAQ
, "Zurich Instruments
Data Acquisition", enables the user to stream data from their instrument
directly into Matlab allowing them to take full advantage of this
powerful environment.
This chapter aims to help you get started using Zurich Instruments
LabOne’s Matlab API, ziDAQ
, to control your instrument.
For a full reference of the Matlab API visit the LabOne API documentation. 
This section and the provided examples are not intended to be a Matlab tutorial. See either MathWorks' online Documentation Center or one of the many online resources, for example, the Matlab Programming Wikibook for help to get started programming with Matlab. 
Installing the LabOne Matlab API
Requirements
One of the following platforms and Matlab versions (with valid license) is required to use the LabOne Matlab API:

32 or 64bit Windows with Matlab R2009b or newer.

64bit Linux with Matlab R2016b or newer.

64bit macOS and Matlab R2013b or newer.
The LabOne Matlab API ziDAQ
is included in a standard LabOne
installation and is also available as a separate package (see below,
Separate Matlab Package). No installation as such
is required, only a few configuration steps must be performed to use
ziDAQ
in Matlab. Both the main LabOne installer and the separate
LabOne Matlab API package are available from Zurich Instruments'
download page.
Separate Matlab Package
The separate Matlab API package should be used if you would like to:

Windows, Linux or Mac
No additional installation steps are required to use ziDAQ
on either
Windows, Linux or Mac; it’s only necessary to add the folder containing
LabOne’s Matlab Driver to Matlab’s search path. This is done as
following:

Start Matlab and either set the "Current Folder" (current working directory) to the Matlab API folder in your LabOne installation or the extracted zip archive of the separate Matlab API package (see above, Separate Matlab Package) as appropriate.
If using a LabOne installation on Windows this is typically:
C:\Program Files\Zurich Instruments\LabOne\API\MATLAB\
and on Linux this is the location where you unpacked the LabOne
.tar.gz
file:[PATH]/LabOne64/API/MATLAB/

In the Matlab Command Window, run the Matlab script
ziAddPath
located in theMATLAB
directory:>> ziAddPath;
On Windows (similar for Linux and Mac) you should see the following output in Matlab’s Command Window:
Added ziDAQ's Driver, Utilities and Examples directories to Matlab's path for this session. To make this configuration persistent across Matlab sessions either: 1. Run the 'pathtool' command in the Matlab Command Window and add the following paths WITH SUBFOLDERS to the Matlab search path: C:\Program Files\Zurich Instruments\LabOne\API\MATLAB\ or 2. Add the following line to your Matlab startup.m file: run('C:\Program Files\Zurich Instruments\LabOne\API\MATLAB\ziAddPath');
This is sufficient configuration if you would only like to use
ziDAQ
in the current Matlab session. 
To make this configuration persistent between Matlab sessions do either one of the next two steps (as also indicated by the output of
ziAddPath
):
Run the
pathtool
and click "Add with Subfolders". Browse to the "MATLAB" directory that was located above in Step 1 and click "OK". 
Edit your
startup.m
to contain the line indicated in the output from Step 2 above. For more help on Matlab’sstartup.m
file, type the following in Matlab’s Command Window:>> docsearch('startup.m')


Verify your Matlab configuration as described in Verifying Successful Matlab Configuration .
Verifying Successful Matlab Configuration
In order to verify that Matlab is correctly configured to use ziDAQ
please perform the following steps:

Ensure that the correct Data Server is running for your HDAWG, HF2 or UHF Instrument (the Data Server on MF Instruments starts when the device is powered on). The quickest way to check is to start the User Interface for your device, see LabOne Software Architecture for more details.

Proceed either of the following two ways:

The easiest way to verify correct configuration is run one of the Matlab API’s examples. In the Matlab command Window run, for example,
example_poll
with your device ID as the input argument:>> example_poll('dev123'); % Replace with your device ID.
If this fails, please try issuing the
connect
command, as described in the next method. 
If a device is not currently available, correct Matlab API configuration can be checked by initializing an API session to the Data Server without device communication.
An API session with the Data Server is created using
ziDAQ’s `connect
(the port specifies which Data Server to connect to on the localhost) cf. Specifying the Data Server Hostname and Port ). In the Matlab command window type one of the following:* `>> ziDAQ('connect', 'localhost', 8005) % 8005 for HF2 Series` * `>> ziDAQ('connect', 'localhost', 8004, 6) % 8004 for HDAWG, UHFLI` * `>> ziDAQ('connect', mfhostname, 8004, 6) % 8004 for MFLI (see below)`
Note, using
'localhost'
above assumes that the Data Server is running on the same computer from which you are using Matlab. See Specifying the Data Server Hostname and Port for information about ports and host names when connecting to the Data Server. For MFLI instruments the hostname/IP address of the MFLI instrument must be provided (the value ofmfhostname
), see Specifying the Data Server Hostname and Port and the Getting Started chapter of the MFLI User Manual for more information.


If no error is reported then Matlab is correctly configured to use
ziDAQ
 congratulations! Otherwise, please try the steps listed in Troubleshooting the LabOne Matlab API .
Getting Started with the LabOne Matlab API
This section introduces the user to the LabOne Matlab API.
Contents of the LabOne Matlab API
Alongside the driver for interfacing with your Zurich Instruments
device, the LabOne Matlab API includes many files for documentation,
utility functions and examples. See the Contents.m
file located in a
LabOne Matlab API directory (see Step 1 in
Windows, Linux or Mac for its
typical location) for a description of the API’s subfolders and files.
Run the command:
>> doc('Contents')
in the Matlab Command Window in the LabOne Matlab API directory or take a look at the LabOne API documentation for a detailed overview.
Matlab Driver Naming
On Windows the MEXfile (the 
Using the Builtin Documentation
To access `ziDAQ’s documentation within Matlab, type either of the following in the Matlab Command Window:
>> help ziDAQ
>> doc ziDAQ
This documentation is located in the file MATLAB/Driver/ziDAQ.m
.
See the
LabOne API documentation
for a detailed overview.
Running the Examples
Prerequisites for running the Matlab examples:

Matlab is configured for
ziDAQ
as described above in Installing the LabOne Matlab API . 
The Data Server program is running and the instrument is discoverable, this is the case if the instrument can be seen in the User Interface.

Signal Output 1 of the instrument is connected to Signal Input 1 via a BNC cable; many of the Matlab examples measure on this hardware channel.
See Contents of the LabOne Matlab API for a list of available examples bundled with the LabOne Matlab API. All the examples follow the same structure and take one input argument: the device ID of the instrument they are to be ran with. For example:
>> example_sweeper('dev123');
The example should produce some output in the Matlab Command Window, such as:
ziDAQ version Jul 7 2015 accessing server localhost 8005.
Will run the example on `dev123`, an `HF2LI` with options `MFKPLLMODRTKPID`.
Sweep progress 9%
Sweep progress 19%
Sweep progress 30%
Sweep progress 42%
Sweep progress 52%
Sweep progress 58%
Sweep progress 68%
Sweep progress 79%
Sweep progress 91%
Sweep progress 100%
ziDAQ: AtExit called
Most examples will also plot some data in a Matlab figure, see Figure 1 for an example. If you encounter an error message please ensure that the Running the Examples are fulfilled and see Troubleshooting the LabOne Matlab API for help troubleshooting the error.
example_sweeper.m
; the plots show the instruments demodulator output when performing a frequency sweep over a simple feedback cable.
The examples serve as a starting point for your own measurement needs. However, before editing the mfiles, be sure to copy them to your own user space (they could be overwritten upon updating your LabOne installation) and give them a unique name to avoid name conflicts in Matlab. 
Using ziCore Modules in the LabOne Matlab API
In the LabOne Matlab API ziCore
Modules are
configured and controlled via Matlab "handles". For example, in order to
use the Sweeper Module a handle is created via:
>> h = ziDAQ('sweep');
and the Module’s parameters are configured using the set
command and
specifying the Module’s handle with a path
, value
pair, for example:
>> ziDAQ('set', h, 'start', 1.2e5);
The parameters can be readback using the get
command, which supports
wildcards, for example:
>> sweep_params = ziDAQ('get', h, '*');
The variable sweep_params
now contains a struct
of all the Sweeper’s
parameters. The other main Module commands are used similarly, e.g.,
ziDAQ('execute', h)
to start the sweeper. See
ziCore Modules
for more help with Modules and a description
of their parameters.
The Data Acquisition Module uses dot notation for subscribing to signals. In the data structure returned by the MATLAB API, the dots are replaced by underscores in order not to conflict with the dot notation used for member selection in MATLAB, e.g. /devNNN/demods/0/sample.r is accessed using devNNN.demods(1).sample_r. 
Enabling Logging in the LabOne Matlab API
Logging from the API is not enabled by default upon initializing a
server session with ziDAQ
, it must be enabled (after using connect
)
with the setDebugLevel
command. For example,
>> ziDAQ('setDebugLevel', 0);
sets the API’s logging level to 0, which provides the most verbose logging output. The other log levels are defined as follows:
trace:0, debug:1, info:2, status:3, warning:4, error:5, fatal:6.
It is also possible for the user to write their own messages directly to
ziDAQ’s log using the `writeDebugLog
command. For example to write a
log message of info
severity level:
>> ziDAQ('writeDebugLog', 1, 'Hello log!');
On Windows the logs are located in the directory
C:\Users\[USER]\AppData\Local\Temp\Zurich Instruments\LabOne
Note that
AppData
is a hidden directory. The easiest way to find it is to open a
File Explorer window and type the text %AppData%\..
in the address
bar, and navigate from there. The directory contains folders containing
log files from various LabOne components, in particular, the ziDAQLog
folder contains logs from the LabOne Matlab API. On Linux, the logs can
be found at "/tmp/ziDAQLog_USERNAME", where "USERNAME" is the same as
the output of the "whoami" command.
LabOne Matlab API Tips and Tricks
In this section some tips and tricks for working with the LabOne Matlab API are provided.
The structure of
ziDAQ commands.All LabOne Matlab API commands are based on a call to the Matlab
function
where the output argument contains the current value of the specified node. To set an integer node value, both the node path and the value to be set must be specified as the second and third arguments:
See the LabOne API documentation for a detailed overview. 
Data Structures returned by
ziDAQ .The output arguments that

The instrument’s node tree uses zerobased indexing; Matlab uses onebased indexing.
See the tip Data Structures returned by 
Explicitly convert
uint64 data types to double .Matlab’s native data type is doubleprecision floating point and doesn’t support performing calculations with with other data types such as 64bit unsigned integers, for example:
Due to this limitation, be sure to convert demodulator timestamps to

Use the utility function
ziCheckPathInData .Checking that a substructure in the nested data structure returned by
can be replaced by:

Troubleshooting the LabOne Matlab API
This section intends to solve possible error messages than can occur
when using ziDAQ
in Matlab.
Error message: "Undefined function or method 'ziDAQ' for input arguments of type '*'"
Matlab can not find the LabOne Matlab API library. Check whether the
and repeating the steps to configure Matlab’s search path in Windows, Linux or Mac . 
Error message: "Undefined function or method 'example_sweeper'"
Matlab can not find the example. Check whether the
and repeating the steps to configure Matlab’s search path in Windows, Linux or Mac . 
Error message: "Error using: ziDAQ ZIAPIException with status code: 32870. Connection invalid."
The Matlab API can not connect to the Data Server. Please check that the correct port was used; that the correct server is running for your device and that the device is connected to the server, see Specifying the Data Server Hostname and Port. 
Error Message: "Error using: ziAutoConnect at 63 ziAutoConnect() failed to find a running server or failed to find a connected a device…"
The utility function

Error Message: "Error using: ziDAQ ZIAPIException on path /dev123/sigins/0/imp50 with status code: 16387. Value or Node not found"
The API is connected to the Data Server, but the command failed to find the specified node. Please:

Error Message: "using: ziDAQ Server not connected. Use 'ziDAQ('connect', …) first."
A
where 
Error Message: "Attempt to execute SCRIPT ziDAQ as a function: ziDAQ.m"
There could be a problem with your LabOne Matlab API installation. The
call to 