MATLAB: External Library version error

!libraryMATLABnetcdfpathsystem command

Hello, I am having some issues with external libraries in Matlab 8.2.0.701 (R2013b) on an iMac OSX 10.9.2 (Mavericks).
I am creating some NetCDF files using the native Matlab interface, which is successful. However, for the last step I use the "system" command to access the NCO (NetCDF Operators) tool "ncatted" to quickly tweak some metadata (add a missing value for all variables) but this fails with the following error:
>> system(['ncatted -O -a missing_value,,c,f,-1.0e34 ',filename])
dyld: Library not loaded: /opt/local/lib/libnetcdf.7.dylib
Referenced from: /opt/local/bin/ncatted
Reason: Incompatible library version: ncatted requires version 10.0.0 or later, but libnetcdf.7.dylib provides version 9.0.0
ncatted -O -a missing_value,,c,f,-1.0e34 psi_data.nc: Trace/breakpoint trap
This is really curious because when I check the versions of the libraries with "ldd" I get:
>> system('ldd /opt/local/bin/ncatted')
/opt/local/bin/ncatted:
/opt/local/lib/libexpat.1.dylib (compatibility version 8.0.0, current version 8.0.0)
/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
/opt/local/lib/libintl.8.dylib (compatibility version 10.0.0, current version 10.2.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
** /opt/local/lib/libnetcdf.7.dylib (compatibility version 10.0.0, current version 10.0.0)
/opt/local/lib/libcurl.4.dylib (compatibility version 8.0.0, current version 8.0.0)
/opt/local/lib/libgsl.0.dylib (compatibility version 18.0.0, current version 18.0.0)
/opt/local/lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/opt/local/lib/libudunits2.0.dylib (compatibility version 2.0.0, current version 2.0.0)
As you can see, libnetcdf.7.dylib () is indeed version 10.0.0. I also get the same problem with other installed commands, e.g.
>> system(['ncdump -h ',filename])
dyld: Library not loaded: /opt/local/lib/libnetcdf.7.dylib
Referenced from: /opt/local/bin/ncdump
Reason: Incompatible library version: ncdump requires version 10.0.0 or later, but libnetcdf.7.dylib provides version 9.0.0
ncdump -h psi_data.nc: Trace/breakpoint trap
I originally thought that the problem was with the libraries in $MATLAB_ROOT/bin/maci64 but as the error message shows, it's not these libraries that are inspected…I even tried linking the /opt/local/ libraries into Matlab but that makes things worse!
Is there some sort of path that is confusing Matlab into testing against a version 9 library? Any ideas would be greatly appreciated. Jonathan

Best Answer

I have found a workaround by altering DYLD_LIBRARY_PATH in my "startup.m" file (placed in ~/Documents/MATLAB):
dylibpath=getenv('DYLD_LIBRARY_PATH');
setenv('DYLD_LIBRARY_PATH',['/opt/local/lib:/usr/local/lib:',dylibpath]);
clear dylibpath
OR in the same file:
setenv('DYLD_LIBRARY_PATH','');
This is only read after Matlab has initialized and so doesn't cause the previous library problems that occur when DYLD_LIBRARY_PATH is set from the terminal. So it seems that Matlab is being a little naughty with setting a restrictive DYLD_LIBRARY_PATH environment, presumably a lazy way to ensure that it's built in libraries are correctly linked to.
I am going to go with the second option and post here any libraries that I find dont work....hopefully they can be tweaked with "install_name_tool". Cheers!
Related Question