sprintf in a for loop
6 次查看(过去 30 天)
显示 更早的评论
Hi :) I'm having trouble creating a for loop for importing a series of netcdf files in Matlab. The only thing that changes each time is the number on the month for each of the 12 datasets, but when I run the loop, it puts the names of the datasets together so won't import them. Here's what I have been using:
A=1:12
for i=1:12
formatspec='filename-month%d-year.nc'
B=(sprintf(formatspec,A))
E=ncread(B,'netcdf_variable')
end
However, when I run this, it comes up with "could not open filename-month1-year.ncfilename-month2-year.ncfilename-month3-year.nc..." etc. Any help would be greatly appreciated, thanks!!
0 个评论
回答(1 个)
Image Analyst
2018-10-13
Try this:
A=1:12
for i=1:12
fileName = sprintf('filename-month%d-year.nc', A(i));
E = ncread(fileName,'netcdf_variable')
end
Or better yet, use the FAQ: https://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
4 个评论
Image Analyst
2018-10-13
I'd use the one in the FAQ that uses dir(). That way you get only files known to exist.
Then you process/analyze E in the loop and store the results somewhere, like in an Excel workbook or text file.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!