Miscellaneous‎ > ‎

Matlab, MEX and NI USB

Are you frustrated because Matlab in OSX doesn't have the Data Acquisition Toolbox?  Does it make you sad that you have a NI USB DAQ and are unable to use it with your Matlab programs?  Well, all hope is not lost. You can suffer through the pain that is MEX files!

I wasn't finding much on the inter-tubes about people sharing there MEX files.  This particular file will probably need to be modified for your application, but at least it is an example to get you started.

The setup:
  • MacBook Pro 2.5 GHz - Core 2 Duo
  • National Instruments USB-6008
  • Macintosh OSX 10.5.8
  • Matlab 2009b (64-bit)
  • NI-DAQmx Base 3.2.0
Goal:
  • Implement NI-DAQmx drivers in a Matlab MEX wrapper for use with m-file scripting
    • Read analog samples from two channels
So what are the challenges to overcome?  It would be nice to have something that was like real-time visualization in Matlab, but this seems challenging.  The first step would to simply read a fixed number of samples from the USB device and output the data to the Matlab workspace.

I start by using example code.  To figure out MEX files I start with the simple timestwo.c file in Matlab.  To determine how to read samples using the NI-DAQmx Base drivers I select the contAcquireNchan.c example.  You can look at these files if you want to deconstruct how I arrived at my MEX file.

Starting with MEX files, the very first thing you will need to do is to setup your compiler.  You can do this by using the following command:

mex -setup

I would suggest compiling a simple example mex file to insure that the compiler is setup correctly. Also, read the documentation to determine how to properly setup the compiler.

You also need to adjust the MEX options manually so that Matlab can find the NI-DAQ Base framework. (See this note).  The options file should be located somewhere like:  

/Users/<USERNAME>/.matlab/R2009b/mexopts.sh 

Adjust your options file (scroll down near the end of the file) to add the blue text:

...
maci)
#----------------------------------------------------------------------------
# StorageVersion: 1.0
# CkeyName: GNU C
# CkeyManufacturer: GNU
# CkeyLanguage: C
# CkeyVersion:
 CC='gcc-4.0'
 SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
 MACOSX_DEPLOYMENT_TARGET='10.5'
 ARCHS='i386'
 CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
 CFLAGS="$CFLAGS -fexceptions"
 CLIBS="$MLIBS -framework nidaqmxbase -framework nidaqmxbaselv"
 COPTIMFLAGS='-O2 -DNDEBUG'
 CDEBUGFLAGS='-g'
#
 CLIBS="$CLIBS -lstdc++"
 # C++keyName: GNU C++
 # C++keyManufacturer: GNU
 # C++keyLanguage: C++
 # C++keyVersion:
 CXX=g++-4.0
 CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
 CXXLIBS="$MLIBS -lstdc++ -framework nidaqmxbase -framework nidaqmxbaselv"
 CXXOPTIMFLAGS='-O2 -DNDEBUG' CXXDEBUGFLAGS='-g'
...

Finally, your version of Matlab must be 32-bit as the NI-DAQ Base drivers are compiled with 32-bit extensions.  If you have a 64-bit version you may be able to switch it to run in 32-bit mode.  You can do this by right-clicking on the Matlab application and selecting "Get Info" in the pop-up menu.  You should have an option to "Open in 32 bit mode".  Now when you open Matlab it should run as "maci".

Now when writing a MEX file the first thing to realize is that it looks like a wrapper.  Every MEX file must have a function called mexFunction and include "mex.h". This function serves as the entry point for Matlab and takes care of the necessary data type wrapping for input and output when used in m-file scripts.  The basic definition of the function looks like:

void mexFunction( int nlhs, mxArray *plhs[], 
                  int nrhs, const mxArray *prhs[] )
{
    ...
}

To compile this file into a MEX object requires including the NI-DAQmx Base header.  On an OSX machine you will type the following:

mex -v contAcquireNchanMatlab.c 
   -I"/Applications/National Instruments/NI-DAQmx Base/Includes"


ċ
contAcquireNchanMatlab.c
(3k)
Jeff Bingham,
May 6, 2010, 7:41 PM
Comments