Borrar filtros
Borrar filtros

iteration on file name

31 visualizaciones (últimos 30 días)
Murstoe
Murstoe el 20 de Abr. de 2020
Comentada: the cyclist el 20 de Abr. de 2020
Hi guys, would anyone be kind enough to explain me this?
So i have to load many datas.
datas are named as follow:
f_1_000_010; f_2_000_020; f_3_000_030; f_4_000_040; f_5_001_000; f_6_001_010; f_7_001_020; f_8_000_030; f_9_000_040; f_10_002_000 ..... and so on.
basically the second term is +1 every time the third term reach 5.
i how do I tell matlab to do that?

Respuesta aceptada

the cyclist
the cyclist el 20 de Abr. de 2020
Editada: the cyclist el 20 de Abr. de 2020
Here is one way:
for nf = 1:10
str1 = sprintf('%d',nf);
str2 = sprintf('%d',mod(nf-1,5)+1);
filename = ['f_',str1,'_000_0',str2,'0'];
end
The key features of the algorithm are
  • use of sprintf to convert the numerics to strings, for a given pattern
  • use of the mod function to implement the "cycle" from 1-5
  • concatenation of strings into one
One doesn't really need to pull the creation of the string into three separate calculations, so this is slicker:
for nf = 1:10
filename = ['f_',sprintf('%d',nf),'_000_0',sprintf('%d',mod(nf-1,5)+1),'0'];
end
but the first way might be easier to understand. The whole thing could also be boiled down to a single use of sprintf:
for nf = 1:10
filename = sprintf('f_%d_000_0%d0',nf,mod(nf-1,5)+1);
end
  3 comentarios
Murstoe
Murstoe el 20 de Abr. de 2020
sorry, i just wanted to understand the reason of it rather than give you my problem to solve it. i wasnt eble to so i asked you again. annoy you wasn't my purpose and i apologize.
it looks like it still deosn't work properly. "f_001_000_080_Int_32F.tif" does not exis". it should be f_002_000_080_Int_32F.tif !.
the cyclist
the cyclist el 20 de Abr. de 2020
No problem.
So, it looks like you have solved your problem of creating the file name string, but have underlying logic issues in how you have implemented the loop and if statements.
Unfortunately, I can't even get your code to run, because of syntax errors.
First, can you confirm that you changed
if j=0
to
if j==0
?
Next, if I do make that change, your code crashes for me because k is not yet defined the first time it tries to create the file name. It is confusing to me why you have both k0 and k defined, and I can't guess at what you are trying to do.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Scripts 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