Error "Could not open file 'ncfiles'". when using a for loop to open ncfiles
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi
I have 469 ncfiles and I open them in a loop:
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ;
for i = 1:Nfiles;
ncdisp(ncfiles(i).name) ;
ncid=netcdf.open('ncfiles','NOWRITE');
varname = netcdf.inqVar(ncid);
[ndim, nvar, natt, unlim]=netcdf.inq(ncid);
end
But when I run the code I got the error when I used ncid:
Error using netcdf.open (line 52)
Could not open file 'ncfiles'.
Error in Blocking (line 9)
ncid=netcdf.open('ncfiles','NOWRITE');
I hope you can help me!
採用された回答
your file name is stored in ncfiles(i).name rather than being the literal file name 'ncfiles'
9 件のコメント
Hmm, I still got the error:
Error using netcdf.open (line 52)
Could not open file 'ncfiles(i).name'.
Error in Blocking (line 11)
ncid=netcdf.open('ncfiles(i).name','NOWRITE');
not 'ncfiles(i).name' just ncfiles(i).name without the '
I just try it before you have writing and it's works - thank!
Hi
Maybe you can tell me how I can define my variables from my nc files?
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ;
for i = 1:Nfiles;
ncdisp(ncfiles(i).name) ;
ncid=netcdf.open(ncfiles(i).name,'NOWRITE');
[ndim, nvar, natt, unlim]=netcdf.inq(ncid);
end
%Defines longitudes, latitudes, time and z
lon = ncread(ncfiles(i).name,'longitude') ; nx = length(lon) ;
lat = ncread(ncfiles(i).name,'latitude') ; ny = length(lat) ;
time = ncread(ncfiles(i).name,'time') ; nt = length(time);
z = ncread(ncfiles(i).name,'z') ; nz = length(z);
Right now I get my different variables:
lat = 181 x 1 (181 values of size 9.9692e + 36)
lon = 360 x 1 (181 values of size 9.9692e + 36)
time = []
z = []
Every iteration you are overwriting ndim and so on . You are also not closing ncid .
The information about variables including their names can be extracted using ncinfo() instead of going into netcdf.* .
You can use dynamic field names to store variables:
loaded_file{i}.(these_variable_names{K})) = ncread(FILENAME, these_variable_names{K});
If you can count on the files all having the same variable names then use (i) instead of {i}
Okay so I try this:
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ;
for i = 1:Nfiles;
ncdisp(ncfiles(i).name) ;
ncid=netcdf.open(ncfiles(i).name,'NOWRITE');
%[ndim, nvar, natt, unlim]=netcdf.inq(ncid);
netcdf.close(ncid);
end
ncvars = {'longitude','latitude','time','z'};
for i = 1:Nfiles
lon(i) = ncread(ncfiles(i).name, ncvars{1}); nx = length(lon(i));
lat(i) = ncread(ncfiles(i).name, ncvars{2}); ny = length(lat(i));
time(i) = ncread(ncfiles(i).name, ncvars{3}); nt = length(time(i));
z(i) = ncread(ncfiles(i).name, ncvars{4}); nz = length(z(i));
end
But now I get the error "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side".
My files have the same variables. The left side and the right side - it is thes sizes in ncread?
Walter Roberson
2018 年 12 月 5 日
編集済み: Walter Roberson
2018 年 12 月 5 日
The result of ncread() is the complete content of the variable you indicate to read. That will not generally be a scalar, but you are assigning it to a scalar location.
You should consider using cell arrays. And watch out because length() is the max() of the dimensions, not any one particular dimension.
Such like that?
%Indlæser alle filer fra folderen
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ;
for i = 1:Nfiles;
ncdisp(ncfiles(i).name) ;
ncid=netcdf.open(ncfiles(i).name,'NOWRITE');
%[ndim, nvar, natt, unlim]=netcdf.inq(ncid);
netcdf.close(ncid);
end
lat = cell(Nfiles, 1);
lon = cell(Nfiles, 1);
time = cell(Nfiles, 1);
z = cell(Nfiles, 1);
for i = 1:Nfiles
lon(i) = ncread(ncfiles(i).name, 'longitude'); %nx = length(lon(i));
lat(i) = ncread(ncfiles(i).name, 'latitude'); %ny = length(lat(i));
time(i) = ncread(ncfiles(i).name, 'time'); %nt = length(time(i));
z(i) = ncread(ncfiles(i).name, 'z'); %nz = length(z(i));
end
Because now I get the error "Conversion to cell from single is not possible."
lon{i} = ...
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Agriculture についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
