Hello, I have an issue in my code to read .netcdf files I try to read it with
temp=ncread('/home/yves/Documents/pre/argo09_14/aoml/1901006/1901006_prof.nc','TEMP');
It works But when I try to create the file first to make it easier to change the file, it doesn't work
rootdir='/home/yves/Documents/pre/argo09_14/aoml/1901006/'
list=dir(rootdir)
a=char(list.name);
filename=(a(5,:))
file = [rootdir, num2str(filename)];
The file created is
/home/yves/Documents/pre/argo09_14/aoml/1901006/1901006_prof.nc
but
temp=ncread(file,'TEMP');
Gets me an error message:
Error using internal.matlab.imagesci.nc/openToRead (line 1252)
Could not open /home/yves/Documents/pre/argo09_14/aoml/1901006/1901006_prof.nc for
reading.
Error in internal.matlab.imagesci.nc (line 122)
this.openToRead();
Error in ncread (line 54)
ncObj = internal.matlab.imagesci.nc(ncFile);
Does anybody know what is wrong with the code? Thank's a lot for the help! Yves

 採用された回答

Steven Lord
Steven Lord 2016 年 6 月 17 日

0 投票

The error message you quoted is:
"Could not open /home/yves/Documents/pre/argo09_14/aoml/1901006/1901006_prof.nc for reading."
Note that there are TWO spaces between the end of the filename and the word "for". When you used "a=char(list.name);" if any of the names of other files in the directory are one character longer than the fifth file's name, that will pad the short name with a space in order to make all the names the same length (so they can fit in a char matrix.) Instead of doing this, go directly to:
filename = list(5).name;
No char matrix creation, no padding, no extra space. [I'd also eliminate the num2str call -- your file name in the struct better be a char vector, or dir has done something wrong.]

1 件のコメント

Yves PONCON
Yves PONCON 2016 年 6 月 20 日
Thank's a lot works perfectly now!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by