Chinese (Simplified)EnglishFrenchItalianPortugueseSpanish

Ocean Optics
Worldwide Headquarters
Largo, Florida, USA

+1 727-733-2447

[email protected]

Sales, Service
& Support Facility
Duiven, The Netherlands

+31 26-319-0500
+33 442-386-588

[email protected]

Ocean Optics GmbH Sales,
Service & Support Facility
Ostfildern, Germany

+49 711-34-16-96-0

[email protected]

Sales Support
for the
United Kingdom

+44 1865-819922

[email protected]

Sales, Service
& Support Facilities
Shanghai, PRC – Beijing, PRC

+86 21-6295-6600

[email protected]

Home > Frequently Asked Questions > Software > OmniDriver and SPAM > Controlling USB spectrometers from OmniDriver and MATLAB

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
  • Was this answer helpful?
  • Yes   No


Cross-Platform Full-Featured Device Driver


Spectral Processing And Math Library