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

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Guillaume
Guillaume el 22 de Nov. de 2016
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'))

Más respuestas (1)

KSSV
KSSV el 22 de Nov. de 2016
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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by