How can I add sequential integers to .mat filename?

조회 수: 13 (최근 30일)
balsip
balsip 2015년 11월 30일
댓글: the cyclist 2015년 12월 2일
I have the following code running within a for loop, which appends a random integer to a .mat filename because it's using "randi". In some instances (random, naturally) this will generate a lower integer to a newer filename. This causes havoc in my data. How can I change this to make the integers increase sequentially with newer files?
dstr=num2str(d);
name=strcat('d',dstr,'_',num2str(randi([0 999])));
I'm betting that this may be as simple as changing "randi" to a function that's not random, but I can't find such a function, and it may have its own syntax peculiarities.
This is what the script is resulting in now, the six-digit number in between "d" and the underscore is a dating convention I need to keep:
  • d736209_021.mat
  • d736210_041.mat
  • d736211_073.mat
  • d736211_090.mat
d736211_073.mat is actually a newer file than d736211_090.mat; this is no good. This is the format I'd like with increasing suffix integers being newer files:
  • d736211_001.mat
  • d736211_002.mat
Thanks in advance.

채택된 답변

the cyclist
the cyclist 2015년 11월 30일
Can you just number them sequentially, like this?
d = 3;
dstr=num2str(d);
n=1;
name=strcat('d',dstr,'_',num2str(n))
n = n+1;
  댓글 수: 8
balsip
balsip 2015년 12월 1일
Cyclist, I owe you an apology and thanks. You were right, the problem was with me not knowing how to implement your answer. I ended up using the following adaptation of your suggestion:
itcount=0;
for i=1:(length(tmp)-1);
itcount=itcount+1;
dstr=num2str(d);
name=strcat('d',dstr,'_',num2str(itcount,'%03i'));
end
The '%03i' gave me the three digit format, so thanks to you, too, Sean. And now I have to go see 'Spectre.'
the cyclist
the cyclist 2015년 12월 2일
No worries. Glad you got it to work.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by