could not open my nc file, error using ncread and loop

this is my code:
files = dir('/media/wrfout_d02_2015-02*');
for i=1:length(files);
filename = files(i).name;
data_ori = double(ncread(filename,'PM10'));
and I got this error;
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open wrfout_d02_2015-02-01_00:00:00 for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);
can anyone help me? what is the error mean? thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 1 月 16 日
編集済み: Walter Roberson 2018 年 1 月 16 日

0 投票

projectdir = '/media';
files = dir( fullfile(projectdir, 'wrfout_d02_2015-02*') );
for i = 1:length(files);
filename = fullfile(projectdir, files(i).name );
data_ori = double(ncread(filename,'PM10'));
end

5 件のコメント

nsyn
nsyn 2018 年 1 月 16 日
oh thanks, it works now
nsyn
nsyn 2018 年 1 月 16 日
oh sorry, Mr. Walter Roberson, there is no error when I am running the code above, but when I check "files" it shows
>> files
files =
0x1 struct array with fields:
name
date
bytes
isdir
datenum
it means the loop command doesn't work, does it? please give any advice, I have checked my directory is right.
Walter Roberson
Walter Roberson 2018 年 1 月 16 日
Did you truly put the files directly in /media ? That would be rather unusual: /media is almost always used to mount filesystems so there would be a mount-point directory name under /media and files would be underneath that.
nsyn
nsyn 2018 年 1 月 16 日
No, I didn't put it directly in media, I just wrote it there to make it simple. in my real code following your instruction above, I write like this:
projectdir = '/media/Seagate1/myaccount/2015output/';
files = dir(fullfile(projectdir,'wrfout_d02_2015_02*'));
for i=1:length(files)
filename = fullfile(projectdir, files(i).name);
data_ori = double(ncread(filename,'PM10'));
end
I have checked the location of the files and it's correct. just for information, when I run my first code like shown in my first question, shown:
>> files
files =
672x1 struct array with fields:
in my opinion, the loops command was correct but the error in "ncread filename", what do you think? please help me to solve it.
Walter Roberson
Walter Roberson 2018 年 1 月 17 日
If you are using R2016b or later (or is it R2017a or later?) you can use
files = dir('/media/Seagate1/myaccount/2015output/wrfout_d02_2015-02*');
for i=1:length(files);
filename = fullfile(files(i).folder, files(i).name);
data_ori = double(ncread(filename,'PM10'));
end

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB Report Generator についてさらに検索

タグ

質問済み:

2018 年 1 月 16 日

コメント済み:

2018 年 1 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by