PDA

View Full Version : reading netcdf in MATLAB



deba
28th July 2005, 04:43 AM
Hi all,
can anyone tell me how to read netcdf files in MATLAB?
also if one has to convert files to netcdf, is it possible to do so in MATLAB.
i am new to this and a detailed description would be highly appreciated.
any links to MATLAB tutorials are also welcome

regards,
deba

vinu
30th July 2005, 02:16 PM
Hi Deba,

I am not specifiying any methods for your question. Although
I hope the similar topic is duscussed in our website. Try these
and I hope it will hlep you.

http://www.oceanographers.net/forums/showpost.php?p=624&postcount=2

http://www.oceanographers.net/forums/showthread.php?t=107


-Thanks

pauloh
10th August 2005, 11:28 AM
You need to have an appropriate toolbox (a good one is mexcdf :http://mexcdf.sourceforge.net/ ).

Once you have installed it, it's pretty easy:
Example:

nc=netcdf('file.nc');
var=nc{'var'}(:);
ans so on!

Good luck!

Paulo

rocksea
10th August 2005, 12:42 PM
thank you pauloh, sorry for missing this and a late reply.
let me detail it.

Warning: If you are using MatLab in linux, see the link given by pauloh and follow instructions there for ‘installing’ interface..

Yes, for accessing netcdf utilities, you've to install a proper interface
like mexcdf. Here I've attached mexcdf53.zip & netcdf_install.m.
Follow the steps below and you'll be able to use netcdf.

---------------------------------------------------------------------

Installing and initiating the netcdf interface (mexcdf) for MatLab. (ver. 5-6, I hope)




copy the file netcdf_install.m into \MatLab\toolbox\
start MatLab
change the working directory to \MatLab\toolbox\
execute netcdf_install;
it will create a dir. netcdf in \toolbox\ in MatLab, File-Set Path, ‘add with sub folders’, select the folder \toolbox\netcdf
exit MatLab
unzip the mexcdf.zip
copy the mexcdf53.dll and netcdf.dll into \toolbox\netcdf\
restart MatLab, select working directory as netcdf
execute tnetcdf; (you’ve to execute tnetcdf every time you need to access netcdf files); a browser opens, you may close it.
---------------------------------------------------------------------------
using the interface with netcdf files.


you can use ncdump in MatLab to find the headers and parameters,, (help ncdump)
now to access a particular parameter, say sst anomaly of sstdata.nc assign the netcdf file to a variable using the command netcdf; (help netcdf) nc = netcdf('F:\studies\oceanography\NOAAData\sstdata.n c','nowrite');
now from the header info through ncdump you know anomaly is assigned the variable name ‘anom’ and the range and the scaling factor etc,,
assign the parameter required to a new variable nsst = nc{'anom'}(5,7,16074:16144); -> anomaly values of year 5, month 7, points 16074:16144
now those anomaly values required is in this variable ‘nsst’,, you can use it for further analyzis,,
-----------------------------------------------------------------------------

deba
16th August 2005, 06:48 AM
Thanks friends. All these will help me to start with. Hope i will be able to get more help on this topic as and when required.

cheers,
deba

leilanegoncalves
23rd April 2007, 08:01 PM
Hi rocksea!
I'm using linux and matlab7. Where I could find a mexcdf.dll file corresponding???

anand
25th April 2007, 01:23 PM
friends,

please do send me if any one have the too to convert netcdf to ascii (windows).

thank you

rocksea
26th April 2007, 01:09 AM
friends,

please do send me if any one have the too to convert netcdf to ascii (windows).

thank you
hello anand, if you are able to read the netcdf file in matlab, you can save it in ascii using the SAVE command with -ascii option. However I feel you want to convert it to ascii without going through reading netcdf directly. I think ncdump is a good utility for that:

Check ncdump at:
http://www.unidata.ucar.edu/software/netcdf/docs/ncdump-man-1.html

Normally you can use it from the dos prompt or even from matlab.

Cheers,,

rocksea
26th April 2007, 01:14 AM
Hi rocksea!
I'm using linux and matlab7. Where I could find a mexcdf.dll file corresponding???

Hello Leilane,

You could find mexnc for linux and other platforms here:
http://mexcdf.sourceforge.net/downloads/
Once you go through the linux link, select the appropriate version of your matlab.
The R14 one may be suitable for your version (7.1). Please check.

Cheers,,

rocksea
11th May 2007, 06:05 AM
link updated

padmanaba Reddy
26th December 2007, 03:02 PM
Dear Oceanographers

I installed mexcdf files in my pc-linux


at end i executed tnetcdf
but i got error like this
Undefined function or variable 'tnetcdf'

can u pls hep me to install mexcdf in detail particularly at add path ...etc

thanks in advance


regards







thank you pauloh, sorry for missing this and a late reply.
let me detail it.

Warning: If you are using MatLab in linux, see the link given by pauloh and follow instructions there for ‘installing’ interface..

Yes, for accessing netcdf utilities, you've to install a proper interface
like mexcdf. Here I've attached mexcdf53.zip & netcdf_install.m.
Follow the steps below and you'll be able to use netcdf.

---------------------------------------------------------------------

Installing and initiating the netcdf interface (mexcdf) for MatLab. (ver. 5-6, I hope)



copy the file netcdf_install.m into \MatLab\toolbox\
start MatLab
change the working directory to \MatLab\toolbox\
execute netcdf_install;
it will create a dir. netcdf in \toolbox\ in MatLab, File-Set Path, ‘add with sub folders’, select the folder \toolbox\netcdf
exit MatLab
unzip the mexcdf.zip
copy the mexcdf53.dll and netcdf.dll into \toolbox\netcdf\
restart MatLab, select working directory as netcdf
execute tnetcdf; (you’ve to execute tnetcdf every time you need to access netcdf files); a browser opens, you may close it.---------------------------------------------------------------------------
using the interface with netcdf files.

you can use ncdump in MatLab to find the headers and parameters,, (help ncdump)
now to access a particular parameter, say sst anomaly of sstdata.nc assign the netcdf file to a variable using the command netcdf; (help netcdf) nc = netcdf('F:\studies\oceanography\NOAAData\sstdata.n c','nowrite');
now from the header info through ncdump you know anomaly is assigned the variable name ‘anom’ and the range and the scaling factor etc,,
assign the parameter required to a new variable nsst = nc{'anom'}(5,7,16074:16144); -> anomaly values of year 5, month 7, points 16074:16144
now those anomaly values required is in this variable ‘nsst’,, you can use it for further analyzis,,-----------------------------------------------------------------------------

tarana
7th January 2008, 11:05 PM
Hi All,

Have a happy new year!

I thank you all for providing the suggestions which is helping me a lot. In fact from your suggestions, I was able to Install and initiate the netcdf interface (mexcdf) for MatLab, but I stuck here now.

I opened my file (prate.nc) in matlab, but not sure which options I have to select from the options. I tried almost all but failed. One final question, it is necessary to use ncdump to read and convert the nc file into ASCII file??? Unfortunately, the "Using interface" part is not very clear to me as I have very little idea about the netcdf and matlab. So would greatly appreciate your help in this regard. I am doing this work in windows and my matlam is 6.5 (R13).

Thank you all once again.










thank you pauloh, sorry for missing this and a late reply.
let me detail it.

Warning: If you are using MatLab in linux, see the link given by pauloh and follow instructions there for ‘installing’ interface..

Yes, for accessing netcdf utilities, you've to install a proper interface
like mexcdf. Here I've attached mexcdf53.zip & netcdf_install.m.
Follow the steps below and you'll be able to use netcdf.

---------------------------------------------------------------------

Installing and initiating the netcdf interface (mexcdf) for MatLab. (ver. 5-6, I hope)



copy the file netcdf_install.m into \MatLab\toolbox\
start MatLab
change the working directory to \MatLab\toolbox\
execute netcdf_install;
it will create a dir. netcdf in \toolbox\ in MatLab, File-Set Path, ‘add with sub folders’, select the folder \toolbox\netcdf
exit MatLab
unzip the mexcdf.zip
copy the mexcdf53.dll and netcdf.dll into \toolbox\netcdf\
restart MatLab, select working directory as netcdf
execute tnetcdf; (you’ve to execute tnetcdf every time you need to access netcdf files); a browser opens, you may close it.---------------------------------------------------------------------------
using the interface with netcdf files.


you can use ncdump in MatLab to find the headers and parameters,, (help ncdump)
now to access a particular parameter, say sst anomaly of sstdata.nc assign the netcdf file to a variable using the command netcdf; (help netcdf) nc = netcdf('F:\studies\oceanography\NOAAData\sstdata.n c','nowrite');
now from the header info through ncdump you know anomaly is assigned the variable name ‘anom’ and the range and the scaling factor etc,,
assign the parameter required to a new variable nsst = nc{'anom'}(5,7,16074:16144); -> anomaly values of year 5, month 7, points 16074:16144
now those anomaly values required is in this variable ‘nsst’,, you can use it for further analyzis,,-----------------------------------------------------------------------------

azizpour
16th February 2008, 10:04 AM
hi dear
i have altimetry data for caspian sea
that's format is: .nc.zp2
what can opean this data?
thank
best regard

bhatt.vihang
12th March 2008, 02:56 AM
dear all,

There is a matlab toolbox to read data from .nc files avaialbe from csrio website

http://www.marine.csiro.au/sw/matlab-netcdf.html

this may be useful to oceanographic community.


Regards,

Vihang

irenechen
2nd December 2008, 05:09 AM
Thanks a lot.

Subrahmanyam
21st May 2009, 03:05 AM
hello,

i followed the steps given by rocksea, i am able to to do all the things. but at last when iexecute tnetcdf; the following error appear

??? Error: File: subsasgn.m Line: 118 Column: 9
"subs" was previously used as a function,
conflicting with its use here as the name of a variable.

Error in ==> netcdf.subsasgn at 83
result = subsasgn(v, s, other);

Error in ==> tnetcdf at 94
f{'time'}.units = 'seconds';

please give me the solution for it.

subrahmanyam

rocksea
21st May 2009, 08:33 AM
hello subrahmanyam, the mexcdf package i had mentioned have been long depreciated. it has been some time since i have used netcdf in matlab. you may have to look for similar packages depending on the version of your matlab (check: http://mexcdf.sourceforge.net/).

also, recent versions of matlab has native support of netcdf, so you dont have to install any extra packages.

Subrahmanyam
23rd May 2009, 02:15 PM
hello Roxy, thanks for ur reply. i am having version 7 of matlab. i tried with the available mexcdf from the link which u given, but not able to open to open it. how to get the 8th version of matlab?

yagnesh
24th May 2009, 06:24 AM
mathworks will give free student version matlab, with limited toolboxes., you can try to get that.,


good luck

prasanth
25th May 2009, 01:06 AM
Yagnesh, I don't think its free either!. I bought Matlab 2007b student version for about 100 dollars. Its cheap in a way.
If Subrahmanyam is using matlab 7 in windows, its just the matter of downloading mexcdf and netcdf toolboxes for matlab 7 version, set the paths in matlab, and copy the .dll files to win32 folder of windows.

Cheers,
Prasanth

Poulomi
16th November 2009, 10:30 AM
After executing tnetcdf at command prompt i'm getting following error:

??? Error: File: C:\MATLAB7\toolbox\netcdf\@ncvar\subsasgn.m Line: 118 Column: 9
"subs" was previously used as a function,
conflicting with its use here as the name of a variable.
Error in ==> netcdf.subsasgn at 83
result = subsasgn(v, s, other);
Error in ==> tnetcdf at 94
f{'time'}.units = 'seconds';

My MATLAB version is 7.0, don't know how to fix the problem, kindly help.













thank you pauloh, sorry for missing this and a late reply.
let me detail it.

Warning: If you are using MatLab in linux, see the link given by pauloh and follow instructions there for ‘installing’ interface..

Yes, for accessing netcdf utilities, you've to install a proper interface
like mexcdf. Here I've attached mexcdf53.zip & netcdf_install.m.
Follow the steps below and you'll be able to use netcdf.

---------------------------------------------------------------------

Installing and initiating the netcdf interface (mexcdf) for MatLab. (ver. 5-6, I hope)




copy the file netcdf_install.m into \MatLab\toolbox\
start MatLab
change the working directory to \MatLab\toolbox\
execute netcdf_install;
it will create a dir. netcdf in \toolbox\ in MatLab, File-Set Path, ‘add with sub folders’, select the folder \toolbox\netcdf
exit MatLab
unzip the mexcdf.zip
copy the mexcdf53.dll and netcdf.dll into \toolbox\netcdf\
restart MatLab, select working directory as netcdf
execute tnetcdf; (you’ve to execute tnetcdf every time you need to access netcdf files); a browser opens, you may close it.
---------------------------------------------------------------------------
using the interface with netcdf files.


you can use ncdump in MatLab to find the headers and parameters,, (help ncdump)
now to access a particular parameter, say sst anomaly of sstdata.nc assign the netcdf file to a variable using the command netcdf; (help netcdf) nc = netcdf('F:\studies\oceanography\NOAAData\sstdata.n c','nowrite');
now from the header info through ncdump you know anomaly is assigned the variable name ‘anom’ and the range and the scaling factor etc,,
assign the parameter required to a new variable nsst = nc{'anom'}(5,7,16074:16144); -> anomaly values of year 5, month 7, points 16074:16144
now those anomaly values required is in this variable ‘nsst’,, you can use it for further analyzis,,
-----------------------------------------------------------------------------

42d3e78f26a4b20d412==