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

조회 수: 3 (최근 30일)
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by