Netcdf interface for Matlab, MEXCDF
Moderators: arango, kate, robertson
 arango
 Site Admin
 Posts: 1128
 Joined: Wed Feb 26, 2003 4:41 pm
 Location: IMCS, Rutgers University
 Contact:
Netcdf interface for Matlab, MEXCDF
Dear All,
We fequently have questions about NetCDF and Matlab. In order to get NetCDF to work in Matlab, you need to intall the MEXCDF interface. For more information about MEXCDF, please visit the following link:
http://woodshole.er.usgs.gov/operations ... excdf.html
If you downloaded my Matlab directory tree which contains a great number of useful Matlab scripts,
http://www.myroms.org/software/Processi ... lab.tar.gz
you need to be careful with the MEX files since they can only be executed on a SUN computer running Solaris. On my computer, these executables have the .mexsol extension.
If you have a different computer architecture, you need to build the appropriate MEXCDF interface. To install the NetCDF toolbox for Matlab, follow the instructions given in:
http://woodshole.er.usgs.gov/staffpages ... c4ml5.html
The SeaGrid generation package also uses MEXCDF files to generate the model grid. It uses the mexinside, mexrec, and mexsepeli programs. You need to build these programs for your computer architecture. See the SeaGrid website for more information:
http://woodshole.er.usgs.gov/staffpages ... agrid.html
Good luck, Hernan
We fequently have questions about NetCDF and Matlab. In order to get NetCDF to work in Matlab, you need to intall the MEXCDF interface. For more information about MEXCDF, please visit the following link:
http://woodshole.er.usgs.gov/operations ... excdf.html
If you downloaded my Matlab directory tree which contains a great number of useful Matlab scripts,
http://www.myroms.org/software/Processi ... lab.tar.gz
you need to be careful with the MEX files since they can only be executed on a SUN computer running Solaris. On my computer, these executables have the .mexsol extension.
If you have a different computer architecture, you need to build the appropriate MEXCDF interface. To install the NetCDF toolbox for Matlab, follow the instructions given in:
http://woodshole.er.usgs.gov/staffpages ... c4ml5.html
The SeaGrid generation package also uses MEXCDF files to generate the model grid. It uses the mexinside, mexrec, and mexsepeli programs. You need to build these programs for your computer architecture. See the SeaGrid website for more information:
http://woodshole.er.usgs.gov/staffpages ... agrid.html
Good luck, Hernan
Use of MexCDF and NetCDF to plot results
I run the upwelling/ downwelling example on a DEC Alpha machine. It generated the files ocean_avg.nc, ocean_dia.nc, ocean_his.nc, and ocean_rst.nc (and log).
I downloaded and installed MexCDF and NetCDF from the website http://woodshole.er.usgs.gov/staffpages ... c4ml5.html. They seem to run within Matlab, open a Net CDF Browser window, but give a couple of error messages about contour3 and feval.
Where can I find examples on the use of MexCDF and NetCDF to read a file (such as ocean_avg.nc, ocean_dia.nc, ocean_his.nc, and ocean_rst.nc) and prepare plots, such as those shown in the SCRUM manual (Figures 1114)?
Thanks,
I downloaded and installed MexCDF and NetCDF from the website http://woodshole.er.usgs.gov/staffpages ... c4ml5.html. They seem to run within Matlab, open a Net CDF Browser window, but give a couple of error messages about contour3 and feval.
Where can I find examples on the use of MexCDF and NetCDF to read a file (such as ocean_avg.nc, ocean_dia.nc, ocean_his.nc, and ocean_rst.nc) and prepare plots, such as those shown in the SCRUM manual (Figures 1114)?
Thanks,
NetCDF in ROMS and MATLAB
Hello all  this is my first post here and I have two questions regarding NetCDF
1) I ran the upwelling test case, and the ocean_his.nc, ocean_avg.nc, ocean_dia.nc, and ocean_rst.nc files are created fine. I can read them in using the ncload command (from the matlab.tar.gz file on this website), but cannot using MATLAB's own cdfread or cdfinfo commands. I thought NetCDF files were supposed to be all in the same format. This is more a curiousity question, as I said that I can read the files using ncload.
2) When I try to plot the results, using speed.m (also from the matlab.tar.gz file), I get errors regarding the missing gname (Grid NetCDF file name). This file is not created during runtime, and I assume that this is the file you create before running ROMS to set up the grid. For these test cases, what should one do (short of plotting the results on your own) to learn how to use the plotting functions, such as speed.m?
Thanks,
Michael
1) I ran the upwelling test case, and the ocean_his.nc, ocean_avg.nc, ocean_dia.nc, and ocean_rst.nc files are created fine. I can read them in using the ncload command (from the matlab.tar.gz file on this website), but cannot using MATLAB's own cdfread or cdfinfo commands. I thought NetCDF files were supposed to be all in the same format. This is more a curiousity question, as I said that I can read the files using ncload.
2) When I try to plot the results, using speed.m (also from the matlab.tar.gz file), I get errors regarding the missing gname (Grid NetCDF file name). This file is not created during runtime, and I assume that this is the file you create before running ROMS to set up the grid. For these test cases, what should one do (short of plotting the results on your own) to learn how to use the plotting functions, such as speed.m?
Thanks,
Michael
I am a new user of ROMS. I installed ROMS in windows XP (via Cygwin). I am using MATLAB 7.1 in my pc. I have some quarry regarding seagrid download instructions mentioned on seagrid homepage.
I am not clear what does mean by “making sure preserve the subdirectory structure”. However, I download and install seagrid and find the directories and subdirectories mentioned on the instructions.
I need to know exactly what is Matlab path, however I added the complete seagrid folder into the toolbox of matlab
In matlab I type cd to the .\seagrid\test_data directory, then type "seagrid" and found the following comments:
## Unable to locate Mexfiles. Please install
## "mexrect", "mexsepeli", and "mexinside",
## then adjust your Matlab path accordingly.
## SeaGrid requires the "Presto" toolbox.
## Please install and/or include in Matlab path.
Can anyone suggest, how to solve the problem?
Rouf
I am not clear what does mean by “making sure preserve the subdirectory structure”. However, I download and install seagrid and find the directories and subdirectories mentioned on the instructions.
I need to know exactly what is Matlab path, however I added the complete seagrid folder into the toolbox of matlab
In matlab I type cd to the .\seagrid\test_data directory, then type "seagrid" and found the following comments:
## Unable to locate Mexfiles. Please install
## "mexrect", "mexsepeli", and "mexinside",
## then adjust your Matlab path accordingly.
## SeaGrid requires the "Presto" toolbox.
## Please install and/or include in Matlab path.
Can anyone suggest, how to solve the problem?
Rouf

 Posts: 36
 Joined: Mon Dec 03, 2007 3:30 pm
 Location: Dalhousie University  Dept. of Oceanography
Hi Rouf,
I am new to ROMS too... but I may be able to help.
I think you need to install MEXNC for your matlab... I wrote a tutorial here: https://www.myroms.org/wiki/index.php/MEXNC
In step #5, I explained a bit how to add a directory to "Matlab's path".
I hope this helps.
Diego
I am new to ROMS too... but I may be able to help.
I think you need to install MEXNC for your matlab... I wrote a tutorial here: https://www.myroms.org/wiki/index.php/MEXNC
In step #5, I explained a bit how to add a directory to "Matlab's path".
I hope this helps.
Diego
Thanks Diego.
I follow the instruction and installed MEXNC and SNCTOOLS but there is a problem with MATLAB toolkit installation.
After installing and extracting Matlab toolkit, I added the directory to the MATLAB path and restart the Matlab. But it shows following comments
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\Damee.
> In path at 113
In startup at 14
In matlabrc at 220
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\coastlines.
> In path at 113
In startup at 15
In matlabrc at 220
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\dx.
> In path at 113
In startup at 16
In matlabrc at 220
…….. similar warning for all subdirectories (floats, forcing, grib, hydro, m_map, presto……) under matlab.
Is there anything wrong? Looking for your suggestion.
Rouf
I follow the instruction and installed MEXNC and SNCTOOLS but there is a problem with MATLAB toolkit installation.
After installing and extracting Matlab toolkit, I added the directory to the MATLAB path and restart the Matlab. But it shows following comments
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\Damee.
> In path at 113
In startup at 14
In matlabrc at 220
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\coastlines.
> In path at 113
In startup at 15
In matlabrc at 220
Warning: Name is nonexistent or not a directory: \home\arango\ocean\matlab\dx.
> In path at 113
In startup at 16
In matlabrc at 220
…….. similar warning for all subdirectories (floats, forcing, grib, hydro, m_map, presto……) under matlab.
Is there anything wrong? Looking for your suggestion.
Rouf

 Posts: 36
 Joined: Mon Dec 03, 2007 3:30 pm
 Location: Dalhousie University  Dept. of Oceanography
Hi Rouf,
In the matlabtoolkit that you just downloaded... there are ~28 directories and 1 file... startup.m
This startup.m file contains a script that adds the path of userspecified directories to Matlab's path... kind of an automatic alternative to the Addpath GUI that you used for MEXNC and SCTOOLS. The problem is that the startup.m file in the matlabtoolkit contains Hernan Arango's paths and NOT yours.
One way to fix this is to edit such startup.m file to reflect YOUR paths... find the following line (in startup.m):
and replace '/home/arango/ocean' with YOUR path to the matlab toolkit.
Another solution (i think) is to delete the startup.m file that is creating the problem (since you already manually added the paths to MEXNC, SNCTOOL, etc )
Let me know what works for you... I may update the tutorial so that future users know what to do with the startup.m file.
Cheers!
Cheers
In the matlabtoolkit that you just downloaded... there are ~28 directories and 1 file... startup.m
This startup.m file contains a script that adds the path of userspecified directories to Matlab's path... kind of an automatic alternative to the Addpath GUI that you used for MEXNC and SCTOOLS. The problem is that the startup.m file in the matlabtoolkit contains Hernan Arango's paths and NOT yours.
One way to fix this is to edit such startup.m file to reflect YOUR paths... find the following line (in startup.m):
Code: Select all
my_root='/home/arango/ocean';
Another solution (i think) is to delete the startup.m file that is creating the problem (since you already manually added the paths to MEXNC, SNCTOOL, etc )
Let me know what works for you... I may update the tutorial so that future users know what to do with the startup.m file.
Cheers!
Cheers
Dear Diego
In terms of Matlab toolkit, I replace the root path in startup.m file but found similar problem and when I delete the startup.m, there is no warning.
My matlab version is 7.1.0.246 (R14) Service Pack 3. That’s why, I chooses the R14sp3 MEXNC version. Then follow the instruction accordingly. In matlab, when I type cd to the .\seagrid\test_data directory, then type "seagrid" But still the following error:
## Unable to locate Mexfiles. Please install
## "mexrect", "mexsepeli", and "mexinside",
## then adjust your Matlab path accordingly.
I search the files in my downloaded Mexnc folder but there are no above named the files.
I also like to inform that my Matlab search path follow the following order: matlab tools, SNCTOOLS, Matlab\bin…Matlab\bin\win32, MEXNC………
So I need your help.
Rouf
In terms of Matlab toolkit, I replace the root path in startup.m file but found similar problem and when I delete the startup.m, there is no warning.
My matlab version is 7.1.0.246 (R14) Service Pack 3. That’s why, I chooses the R14sp3 MEXNC version. Then follow the instruction accordingly. In matlab, when I type cd to the .\seagrid\test_data directory, then type "seagrid" But still the following error:
## Unable to locate Mexfiles. Please install
## "mexrect", "mexsepeli", and "mexinside",
## then adjust your Matlab path accordingly.
I search the files in my downloaded Mexnc folder but there are no above named the files.
I also like to inform that my Matlab search path follow the following order: matlab tools, SNCTOOLS, Matlab\bin…Matlab\bin\win32, MEXNC………
So I need your help.
Rouf

 Posts: 36
 Joined: Mon Dec 03, 2007 3:30 pm
 Location: Dalhousie University  Dept. of Oceanography
Hi Rouf,
Unfortunately I haven't work with Seagrid (I tried, but I also ran into troubles), so I won't be able to help a lot more on this ...Hernan said in the begining of this thread that
.\seagrid\mex_<version>_<os> directory to your matlab path. " . Since you are on a PC it looks like .\seagrid\mex is the appropriate directory to add to the path. If that doesn't work... you may have to compile the MEX files for your Matlab version from source code... tricky for us new users.
You may want to try https://www.myroms.org/wiki/index.php/easygrid
It is not nearly as powerful as seagrid, but it is easier to get upandrunning. I am still working on this tutorial, but it may be finishedenough to be helpful for you.
Back to seagrid... Perhaps a more advanced user can suggest Rouf how to fix his seagrid problem?
Unfortunately I haven't work with Seagrid (I tried, but I also ran into troubles), so I won't be able to help a lot more on this ...Hernan said in the begining of this thread that
I can tell you already visit the website above and read the instructions. There it says to add the " appropriateIt uses the mexinside, mexrec, and mexsepeli programs. You need to build these programs for your computer architecture. See the SeaGrid website for more information:
http://woodshole.er.usgs.gov/staffpages ... agrid.html
.\seagrid\mex_<version>_<os> directory to your matlab path. " . Since you are on a PC it looks like .\seagrid\mex is the appropriate directory to add to the path. If that doesn't work... you may have to compile the MEX files for your Matlab version from source code... tricky for us new users.
You may want to try https://www.myroms.org/wiki/index.php/easygrid
It is not nearly as powerful as seagrid, but it is easier to get upandrunning. I am still working on this tutorial, but it may be finishedenough to be helpful for you.
Back to seagrid... Perhaps a more advanced user can suggest Rouf how to fix his seagrid problem?
Sorry I did a mistake; I didn’t add the seagrid folders into Matlab path properly. Now I overcome the abovementioned problem. Thanks Diego.
I am following Signell_seagrid tutorial. I extract, process and save the coast file and bathy file properly. My computer is also able to open the coast and bathy file properly in seagrid. But when I tried to make grid with 4 boundary points, I found following error in matlab
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
.
Error in ==> mexrect2grid at 98
zrect = feval('mexrect', zrect, length(zrect), ...
Error in ==> seagrid.gridcalc at 179
[w, err] = feval(theFcn, zti, [], ci, [m n]); % <Error> seagrid.dogrid at 64
gridcalc(self) % <Calculate> seagrid.doupdate at 92
dogrid(self, needsUpdate); % <== Step #2 <Error> seagrid.getboundary at 109
doupdate(self, 1)
Error in ==> seagrid.doevent at 129
getboundary(self, theCommand, theMessage)
Error in ==> psevent at 25
doevent(ps(gcbf), theEvent, theMessage);
??? Error while evaluating figure WindowButtonDownFcn.
can anyone help me to overcome the problem?
Rouf
I am following Signell_seagrid tutorial. I extract, process and save the coast file and bathy file properly. My computer is also able to open the coast and bathy file properly in seagrid. But when I tried to make grid with 4 boundary points, I found following error in matlab
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
.
Error in ==> mexrect2grid at 98
zrect = feval('mexrect', zrect, length(zrect), ...
Error in ==> seagrid.gridcalc at 179
[w, err] = feval(theFcn, zti, [], ci, [m n]); % <Error> seagrid.dogrid at 64
gridcalc(self) % <Calculate> seagrid.doupdate at 92
dogrid(self, needsUpdate); % <== Step #2 <Error> seagrid.getboundary at 109
doupdate(self, 1)
Error in ==> seagrid.doevent at 129
getboundary(self, theCommand, theMessage)
Error in ==> psevent at 25
doevent(ps(gcbf), theEvent, theMessage);
??? Error while evaluating figure WindowButtonDownFcn.
can anyone help me to overcome the problem?
Rouf
You should check the Matlab Release Notes:
I'd need to know your Matlab version to be sure, but I suspect you have a recent version that does not recognize the .dll files.New File Extension for MEXFiles on Windows® Systems
MATLAB now uses the extension .mexw32 for MEXfiles on 32bit versions of Windows systems. In previous versions, MATLAB used the extension .dll.
John Wilkin: DMCS Rutgers University
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
My Matlab version is 7.1. If I add mex_matlab71_win32 in the matlab path and try to make grid with 4 boundary points, it shows following error:
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
If I add mex_74_win32 in Matlab path it shows following
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32': Access is denied.
I need to know which mex_matlab??_win32 should I add with my Matlab path?
I check the Matlab Release Notes. I suspect (but not sure) that I have to recompile all MEXfiles. There are lot of mex files here in Matlab. Is it mean I have to recompile all or only mexrect", mexsepeli, and mexinside?
I will be pleased if someone let me know details how to recompile the files in Matlab? Or any other way to overcome the problem as I am not sure wheather its a recompiling issue or not?
Rouf
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
If I add mex_74_win32 in Matlab path it shows following
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32': Access is denied.
I need to know which mex_matlab??_win32 should I add with my Matlab path?
I check the Matlab Release Notes. I suspect (but not sure) that I have to recompile all MEXfiles. There are lot of mex files here in Matlab. Is it mean I have to recompile all or only mexrect", mexsepeli, and mexinside?
I will be pleased if someone let me know details how to recompile the files in Matlab? Or any other way to overcome the problem as I am not sure wheather its a recompiling issue or not?
Rouf
The ROMS User forum might not be the best place to debug your Matlab problems, but let's try anyway:
What are the results of the following commands in your matlab environment:
>> version
>> which seagrid
>> seagrid('version')
>> which mexrect
>> test_mexrect
What are the results of the following commands in your matlab environment:
>> version
>> which seagrid
>> seagrid('version')
>> which mexrect
>> test_mexrect
John Wilkin: DMCS Rutgers University
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
My Matlab environment results:
>> version
ans =
7.1.0.246 (R14) Service Pack 3
>> seagrid('version')
Version of 10Aug2001 14:37:19.
>> which mexrect
C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32
>> test_mexrect
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32': Access is denied.
.
Error in ==> test_mexrect at 35
z = mexrect ( z, np, n(1), n(2), n(3), n(4) );
Rouf
>> version
ans =
7.1.0.246 (R14) Service Pack 3
>> seagrid('version')
Version of 10Aug2001 14:37:19.
>> which mexrect
C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32
>> test_mexrect
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab74_win32\mexrect.mexw32': Access is denied.
.
Error in ==> test_mexrect at 35
z = mexrect ( z, np, n(1), n(2), n(3), n(4) );
Rouf
You have Matlab version 7.1.0.246 (R14) Service Pack 3 so I believe you should be using the 'dll' mex file, not the mexw32.
Edit your matlabpath, verify with "which mexrect" that matlab finds the correct file, and try again with test_mexrect.
Edit your matlabpath, verify with "which mexrect" that matlab finds the correct file, and try again with test_mexrect.
John Wilkin: DMCS Rutgers University
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
Thanks Wilkin. This time I use the dll mex file, edit my matlab path accordingly but following error:
>> cd mex_matlab71_win32
>> which mexrect
C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll
>> test_mexrect
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
.
Error in ==> test_mexrect at 35
z = mexrect ( z, np, n(1), n(2), n(3), n(4) );
Rouf
>> cd mex_matlab71_win32
>> which mexrect
C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll
>> test_mexrect
??? Invalid MEXfile 'C:\cygwin\seagrid\mex_matlab71_win32\mexrect.dll': Access is denied.
.
Error in ==> test_mexrect at 35
z = mexrect ( z, np, n(1), n(2), n(3), n(4) );
Rouf
I asked John Evans at The Mathworks about this, and he is as mystified as me. Here is his reply:
So I suggest you try John's new mex files posted on sourceforge where you got the files before, and let us know what happens.Uhm, hmm, my first thought was that the user must be using an old mexfile,
because the 7.1 release is supposed to be using "*.mexw32" instead of "*.dll"
for an extension... but it looks like I was the one who built it as a *.dll
in the first place. Looking a bit further, it seems that dropping the dll
extension is strongly recommended, but not required for 7.1. It *should*
still work, though.
I went ahead and rebuilt the win32 mexfile for 7.1 (R14sp3) as having
the "*.mexw32" extention. It tests ok and is posted at the sourceforge site.
Tell the user to try it out.
John Wilkin: DMCS Rutgers University
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
71 Dudley Rd, New Brunswick, NJ 089018521, USA. ph: 6096300559 jwilkin@rutgers.edu
Re: Netcdf interface for Matlab, MEXCDF
New from version R2008b, Matlab now supports its own netcdf api.
No need any more for Mexnc etc !
See http://www.mathworks.com/access/helpdes ... r9v1.html
No need any more for Mexnc etc !
See http://www.mathworks.com/access/helpdes ... r9v1.html