iteration on file name
26 ビュー (過去 30 日間)
古いコメントを表示
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
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 件のコメント
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!