MATLAB fails to read MEX file

In the institution I work with MATLAB there was recently a system update (debian).
Before this I could read GRIB files with the NetCDF/GRIB reader function cdi_readfile.
After the update this isn't possible no longer. A code snippet would look like:
data=cdi_readfield(<path to file>,[],'var100');
I have tried this with MATLAB R2010b and R2013b. Both times the same error message:
??? Attempt to execute SCRIPT cdi_readfield as a function:
<some path>/matlab-cdi/cdi_readfield.m
In the matlab-cdi folder there is a cdi_readfield.m which contains only comments and the MEX file cdi_readfield.mexglx
Apparently MATLAB only "see" cdi_readfield.m and not the MEX file. Hence it fails to read the GRIB file.
So I guess it has something to do with the update of the OS.
Did somebody make a similar experience or can give a hint what could be the reason?
Thanks

Answers (2)

At the command line give the command
rehash toolboxcache

2 Comments

Thanks Walter, but unfortunately this doesn't change anything.
The mexglx extension appears to be for 32 bit Linux. It is possible that you upgraded to 64 bit Linux, which would use mexa64 as the extension.
You should go back to the cdi directory and use the "compile" function to create a .mexa64 for use on your system.

Sign in to comment.

Steven Lord
Steven Lord on 17 Jul 2015
The extension .mexglx is the correct extension for 32-bit Linux MEX-files. As part of the OS upgrade, did you upgrade to 64-bit Linux? If so, you will need to rebuild the MEX-file (or if you don't have the source code, ask the person from whom you obtained the MEX-file to provide you with a new file.) The new MEX-file should have the extension .mexa64 as described in the documentation for MEXEXT.

9 Comments

You're right. It was an upgrade to 64-bit Linux.
The MEX files I got with the above mentioned NetCDF/GRIB reader.
So I guess it works only for 32 bit systems, or at least has no options for 64 bit systems.
No, the .mex* files are outputs of the mex process. The inputs are the .c and .h files. Use the compile.m provided to generate a .mex* for your system.
Steve, the information about .mexlgx is oddly difficult to find. Searching there are hits from users listing it variously as 32 bit Linux or 64 bit Linux. I happened to remember the mexext command and checked the documentation for it, which does not mention 32 bit Linux or mexglx, so I had to deduce that mexglx was the 32 bit version based upon a specific other name being given for 64 bit Linux.
As 32 bit Linux is no longer a platform for MATLAB this is historical information, but it would still have been better if it had been documented in current documents, even if only in the notes for mexext
Walter, I thought that and tried to compile again. I got the following error:
COMPILING cdi_varlist...
Warning: You are using gcc version "4.7.2-5)". The version
currently supported with MEX is "4.3.4".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
cdi_mx.c: In function getLevel:
cdi_mx.c:431:43: warning: incompatible implicit declaration of built-in function ‘modf’ [enabled by default]
/usr/bin/ld: cannot find -lcdi
collect2: error: ld returned 1 exit status
mex: link of ' "cdi_varlist.mexa64"' failed.
I guess I have to change the default compiler used by the mex command.
The command
mex -setup
provides me the path to the options file for building gcc MEX-files "gccopts.sh". In line 103 (for glnxa64 / Linux 64 bit) there is the option:
CC='gcc'
which links to the standard C compiler on our system which is gcc-4.7
My question: How should I change this line in order to make gcc-4.3 the compiler MATLAB uses?
install gcc-4.3 and then change the line to
CC='gcc-4.3'
However, that part is a warning and the error is the lack of -lcdi so the cdi library needs to be built.
Look in the INSTALL file. It says
Pre-requisites -------------- Check if you have the netCDF and CDI libraries on your system (usually these are called libnetcdf.a and libcdi.a). If not, you need to install these libraries before continuing.
netCDF: available from http://www.unidata.ucar.edu/software/netcdf (matlab-cdi has been tested only with netcdf 3.x, netcdf 4.0 may or may not work)
CDI: available from http://www.mpimet.mpg.de/cdi
"the error is the lack of -lcdi so the cdi library needs to be built."
The line in the compile.m file that invokes the mex command is:
mex cdi_varlist.c cdi_mx.c -Iinclude -Llib64 -lcdi -lnetcdf
Since my folder lib64 did not contain the netCDF and CDI libraries, I copied them from the folder lib32 (when everything was working on the old 32-bit system) into lib64. Then (fingers crossed) I compiled again. Now I get a slightly different error message:
/usr/bin/ld: skipping incompatible lib64/libcdi.a when searching for -lcdi
/usr/bin/ld: cannot find -lcdi
/usr/bin/ld: skipping incompatible lib64/libnetcdf.a when searching for -lnetcdf
collect2: error: ld returned 1 exit status
I guess now I have to compile the netCDF and CDI libraries again for the 64 bit system.
Thanks for the help.
Yes you will need to rebuild netCDF and CDI for 64 bit.
Update: After I took 64-bit builds of netCDF and CDI libraries I tried to compile again with MATLAB R2013b (hence no gcc compiler warning).
Now I get the following errors:
COMPILING cdi_varlist...
cdi_mx.c: In function ‘getLevel’:
cdi_mx.c:431:43: warning: incompatible implicit declaration of built-in function ‘modf’ [enabled by default]
cdi_mx.o: In function `getLevel':
cdi_mx.c:(.text+0x78c): undefined reference to `Warning'
lib64/libcdi.a(cdf.o): In function `hdfLibraryVersion':
(.text+0x24): undefined reference to `H5get_libversion'
lib64/libcdi.a(gribapi.o): In function `gribapiLibraryVersion':
(.text+0x5): undefined reference to `grib_get_api_version'
lib64/libcdi.a(stream_gribapi.o): In function `gribapiGetValidityDateTime':
(.text+0x27): undefined reference to `grib_get_long'
.....
.....
lib64/libnetcdf.a(libnetcdf4_la-nc4attr.o): In .function `NC4_del_att':
(.text+0x157f): undefined reference to `H5Adelete'
collect2: error: ld returned 1 exit status
mex: link of ' "cdi_varlist.mexa64"' failed.
I'm not completely sure, if there is still a problem with the libraries. Or if the NetCDF/GRIB reader is just outdated, and not usable for 64-bit systems.

Sign in to comment.

Asked:

on 17 Jul 2015

Commented:

on 20 Jul 2015

Community Treasure Hunt

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

Start Hunting!