How can I access Ocean Optics Spectrometers from MatLab?

posted in: Software & Drivers | 0


Ocean Optics Spectrometer Support from MATLAB and Instrument Control Toolbox – Hardware Support

Ocean Optics Spectrometer Support from Instrument Control Toolbox

Acquire data and control Ocean Optics spectrometers with MATLAB.

MATLAB Support Package for use with Instrument Control Toolbox

MatLab offers driver support for our spectrometers, so you can acquire data and control the spectrometer directly from MatLab.

If you prefer to write your own code you can use Omnidriver Java jar files:

Controlling USB spectrometers from OmniDriver and MATLAB – Ocean Optics

Controlling USB spectrometers from OmniDriver and MATLAB

Want to use MathWorks MATLAB to directly control your USB spectrometers, reading and post-processing spectra from .m files? No worries, MATLAB and OmniDriver are old friends.

OmniDriver includes a file in OOI_HOME called OmniDriver.jar, which provides the complete class structure of OmniDriver to any program which can utilize Java JAR files — including MATLAB! Calling the OmniDriver Wrapper can be as simple as this:

>> javaaddpath('C:\Program Files\Ocean Optics\OmniDriver\OOI_HOME\OmniDriver.jar');
>> wrapper = com.oceanoptics.omnidriver.api.wrapper.Wrapper();
>> wrapper.openAllSpectrometers();
>> spectrum = wrapper.getSpectrum(0);
>> wavelengths = wrapper.getWavelengths(0);
>> plot(wavelengths, spectrum);


Feel free to explore the full Java Wrapper API as documented here:

One note: MATLAB may complain about having trouble finding the native USB device drivers…something like this:

            at com.oceanoptics.uniusb.USB.<init> (
            at com.oceanoptics.omnidriver.features.USBImpl.<init> (
                 (blah blah blah)
            at com.oceanoptics.omnidriver.api.wrapper.Wrapper.openAllSpectrometers
Caused by: java.lang.RuntimeException: Failed to load any native library for USB.  
                 Tried base filenames: NatUSB_64, NatUSB_32, NatUSB
            at com.oceanoptics.uniusb.UniUSB.<clinit>(

What this means is that MATLAB’s JNI (Java Native Interface) can’t find the .DLL (or .so or .dylib for Linux and Mac) “native libraries” which OmniDriver uses to talk over USB on each operating system. And unfortunately, there is no way to dynamically “re-point” MATLAB to look at a new path after MATLAB launches (this is not the same as CLASSPATH, nor is it the same as LD_LIBRARY_PATH).

Fortunately, you can follow a quick and simple procedure to update MATLAB’s JNI path between MATLAB sessions:

  1. Launch MATLAB
  2. Type “mr = matlabroot” (this should display the path to matlabroot on your installation)
  3. Close MATLAB
  4. Open matlabroot/toolbox/local/librarypath.txt
    1. (you may have to manually open Accessories -> WordPad from the Start Menu by right-clicking “Run As Administrator”)
  5. Add path to OOI_HOME on your installation (e.g. C:\Program Files\Ocean Optics\OmniDriver\OOI_HOME)
  6. Relaunch MATLAB

To learn more about matlabroot, see these links (valid at writing):

To time operations in MATLAB, just use this:

% initialize spectrometer
wrapper = com.oceanoptics.omnidriver.api.wrapper.Wrapper();

% set integration time to 1 millisec (1000 microsec)
wrapper.setIntegrationTime(0, 1000);

% take 10,000 spectra
time_start = datestr(now,'dd-mm-yyyy HH:MM:SS.FFF');
for i = 1:10000
  spectrum = wrapper.getSpectrum(0);
time_end = datestr(now,'dd-mm-yyyy HH:MM:SS.FFF');

% display the time it took