Can I change Matlab libraries in a program

I am trying to run a FORTRAN program (FVCOM) from Matlab. I ran into the error similar to what these folks had:
My FORTRAN program uses a library called libnetcdf.so.7. Matlab has a built in version of this library, but it is missing a symbol that my FORTRAN program needs. So in order for me to successfully execute my program from MATLAB I need to start up Matlab using the following command from the terminal:
LD_PRELOAD=/usr/local/lib/libnetcdf.so.7 matlab
However, before I run my FORTRAN program I want to use Matlab's netdcf abilities to create some input files needed for the program. Unfortunately, since I changed where Matlab is looking for the libnetcdf.so.7, Matlab's netcdf functions don't work because they need a symbol that is in the built in Matlab library and not in the library that is in my /user/local/lib/ directory.
Is there a way for me to set the library path after matlab has started? Ideally I would start up matlab normally (without changing the library path) and do something like:
%%%execute matlab code using matlab's netcdf functions such as...%%%
t=ncread(file, 'time');
nc=netcdf.create(WindFile,'clobber'); % etc
%%%then, in the same instance of Matlab run my Fortran program %%%
% change my library path, but note that this doesn't work
LD_LIBRARY_PATH=/usr/local/lib/libnetcdf.so.7
% run my FORTRAN program
system(sprintf('sh run_FVCOM.sh'))
I am running Matlab R2014a on Ubunto 14.04.1
It seems like I can either startup Matlab to run my Fortran program or I can startup Matlab normally to create my netcdf files, but not both. The only work-around I can think of is to call matlab from matlab (which just seems really kludgy):
%%%code to create my netcdf files %%%
nc=netcdf.create(WindFile,'clobber'); % etc
%%%code to create a new instance of matlab that calls a program to run my fortran program %%
system(LD_PRELOAD=/usr/local/lib/libnetcdf.so.7 matlab -nodisplay -r "runfvcom")
Is there a more ellegant solution that I am not aware of?

 采纳的回答

Sean de Wolski
Sean de Wolski 2014-12-30
编辑:Sean de Wolski 2014-12-30
Are you using setenv (from within MATLAB) to set LD_LIBRARY_PATH to the environment variable? Just having it in the workspace will do nothing.

1 个评论

I had tried setenv, but I was not using the correct syntax. However, trying it correctly fixed my problem and it is much more elegant than starting an entirely new instance of matlab.
!ldd fvcom
&nbsp &nbsp libnetcdf.so.7 => /usr/local/MATLAB/R2014a/bin/glnxa64/libnetcdf.so.7
setenv('LD_LIBRARY_PATH', '/usr/local/lib/libnetcdf.so.7')
!ldd fvcom
&nbsp libnetcdf.so.7 => /usr/local/lib/libnetcdf.so.7
@Sean: Thank you. All of my searches on changing the library paths said I either had to delete/move the library (so that Matlab is forced to find it in /usr/local/lib/..) or to start matlab using LD_PRELOAD. I guess I wasn't looking in the right place, since this seems like a pretty easy and (in hindsight) obvious fix.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB Mobile 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by