looping through files, filenames varying by number in name gives num2str error

3 ビュー (過去 30 日間)
Marilena Geng
Marilena Geng 2016 年 11 月 22 日
コメント済み: Marilena Geng 2016 年 11 月 22 日
Hello everyone, so I wanna do this really simple loop:
for i = 5:9
A = squeeze(nc_varget('/home/forsker1/Documents/GL2/2001-2010/DMI-HIRHAM5_GL2_2001_' num2str(i) '_maskgreenlandglacier.nc','tas'));
%do something
end
but it keeps underlining the num2str(i) and the second last bracket, with the note "invalid syntax, possibly )]} missing". And when I run it I get the error "unexpected Matlab expression". I'm sorry,I know this should be so easy but I just don't get what I'm doing wrong here and it drives me crazy.

採用された回答

Guillaume
Guillaume 2016 年 11 月 22 日
You need to tell matlab that you're concatenating strings, thus wrap your concatenation in []:
['/home/forsker1/Documents/GL2/2001-2010/DMI-HIRHAM5_GL2_2001_' num2str(i) '_maskgreenlandglacier.nc']
or in my opinion, better yet, use sprintf:
A = squeeze(nc_varget(sprintf('/home/forsker1/Documents/GL2/2001-2010/DMI-HIRHAM5_GL2_2001_%d_maskgreenlandglacier.nc', i),'tas'))

その他の回答 (1 件)

KSSV
KSSV 2016 年 11 月 22 日
for i = 5:9
A = squeeze(nc_varget(strcat('/home/forsker1/Documents/GL2/2001-2010/DMI-HIRHAM5_GL2_2001_', num2str(i), '_maskgreenlandglacier.nc'),'tas'));
%do something
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by