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 日

0 投票

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 件のコメント

the cyclist
the cyclist 2020 年 4 月 20 日
"but what if i have something like this instead"
My immediate and emotional reaction to that phrase is something like ...
  • Did I solve your first problem, and this is a new problem?
  • If yes, then maybe accept this solution, and start a new question.
  • If not, why did you waste our time and not just post this in the first place?
OK, so now I've processed my emotions.
My next reaction is a mix of emotional and rational:
If you really take the time to understand my solution, I'd really think you ought to be able to solve this problem. Have you?
Finally, and probably most helpfully, I notice that you probably want
if j==0
...
end
instead of
if j=0
end
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 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2020 年 4 月 20 日

コメント済み:

2020 年 4 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by