How can I add sequential integers to .mat filename?

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.

 Accepted Answer

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 Comments

Thanks, Cyclist, but that's not quite hitting it. I've updated the question with more background info.
Your solution returned "d3_1.mat", overwriting each successive file with the same filename. I'm sure that has something to do with the for loop I forgot to mention initially.
See what you think of it with the new info I've added.
Thanks!
Change the format in the second num2str to '%03i', James Bond's favorite format.
num2str(7,'%03i')
ans =
007
I don't understand why my solution doesn't help you, balsip. Just increment n after every time you write the file, just like you are currently using randi() every time.
for d = [3 4 5]
dstr=num2str(d);
n = 1;
for m = [7 8 9]
name=strcat('d',dstr,'_',num2str(n))
n = n+1;
end
end
Or maybe I am really missing the point.
Thanks for the input, everyone. It's very likely that I'm leaving something crucial out.
I'm executing this within a larger script, and it's unfeasible for me to change it for every file it's importing. So changing "n" for every file wouldn't work because I'm importing hundreds of file with one hit of enter.
Also, the vectors get erased at the end of the script each time it runs, and it's coded to run for however many files there are in a folder.
But again, thanks for the attempt. Unless I've given you a bit of info you needed, I'll try to reconfigure how I'm asking this question so it makes more sense to those of you not in my head.
OK. My gut says that somehow you can always embed a simple counter (even if you need to resort to a global variable that sits atop your outermost script), that increments every time you need to create a new file.
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.'
No worries. Glad you got it to work.

Sign in to comment.

More Answers (0)

Asked:

on 30 Nov 2015

Commented:

on 2 Dec 2015

Community Treasure Hunt

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

Start Hunting!