Q:

Library not loaded: @loader_path/libmex.dylib in matlab

macos high sierra 10.13.1 xcode 9.2 matlab 2017b
while running a program in mac matlab in 2017b version, I tried to run pmtk3 from this link and while running I got the following error while running second command

Steps: 

run initPmtk3.m (takes a few minutes) 
run testPmtk3.m (takes under a minute) 
run runDemos.m (takes about 1 hour)

error:
'/Users/laxmikadariya/Documents/MATLAB/pmtk3-master/pmtksupportCopy/markSchmidt-9march2011/markSchmidt/minFunc/lbfgsC.mexmaci64':
dlopen(/Users/laxmikadariya/Documents/MATLAB/pmtk3-master/pmtksupportCopy/markSchmidt-9march2011/markSchmidt/minFunc/lbfgsC.mexmaci64,
6): Library not loaded: @loader_path/libmex.dylib
  Referenced from:
  /Users/laxmikadariya/Documents/MATLAB/pmtk3-master/pmtksupportCopy/markSchmidt-9march2011/markSchmidt/minFunc/lbfgsC.mexmaci64
    Reason: image not found.

How can I solve this issue in matlab mac?
I tried to set DYLD_LIBRARY_PATH in .bash_profile as 
DYLD_LIBRARY_PATH='/Applications/MATLAB_R2017b.app/bin/maci64:/Applications/MATLAB_R2017b.app/sys/os/maci64'
this couldnot solve the problem

A:

The lbfgsC.mexmaci64 file comes pre-compiled, and with a time-stamp from 2012. Many things have changed on MacOS since then... :)
In a terminal window, I tried:
$> otool -L lbfgsC.mexmaci64
lbfgsC.mexmaci64:
    @loader_path/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
    @loader_path/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
    @loader_path/libmat.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)

As you can see, it references MATLAB's libraries using @loader_path, which is wrong. That should be @rpath.
I tried recompiling the MEX-file, from the MATLAB command prompt:
>> cd pmtk3/pmtksupportCopy/markSchmidt-9march2011/markSchmidt/minFunc
>> mex -compatibleArrayDims lbfgsC.c

The -compatibleArrayDims option is necessary because the code is very old, and uses int for array sizes (32 bits), rather than mwSize (64 bits).
In a terminal window I now see:
$> otool -L lbfgsC.mexmaci64
lbfgsC.mexmaci64:
    @rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)

This looks a lot better, it's using @rpath as it should. The MEX-file now ran, meaning that the linker problem is solved.
There is another MEX-file in this same directory, it will have the same problem. You'll have to compile that one at the same time:
>> mex -compatibleArrayDims mcholC.c

