iteration on file name

조회 수: 18 (최근 30일)
Murstoe
Murstoe 2020년 4월 20일
댓글: the cyclist 2020년 4월 20일
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?

채택된 답변

the cyclist
the cyclist 2020년 4월 20일
편집: the cyclist 2020년 4월 20일
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
Murstoe
Murstoe 2020년 4월 20일
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 2020년 4월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by