iteration on file name
15 次查看(过去 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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!