Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

string - matlab iterative filenames for saving

this question about matlab: i'm running a loop and each iteration a new set of data is produced, and I want it to be saved in a new file each time. I also overwrite old files by changing the name. Looks like this:

name_each_iter = strrep(some_source,'.string.mat','string_new.(j).mat')

and what I#m struggling here is the iteration so that I obtain files: ...string_new.1.mat ...string_new.2.mat etc.

I was trying with various combination of () [] {} as well as 'string_new.'j'.mat' (which gave syntax error)

How can it be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Strings are just vectors of characters. So if you want to iteratively create filenames here's an example of how you would do it:

for j = 1:10,
   filename = ['string_new.' num2str(j) '.mat'];
   disp(filename)
end

The above code will create the following output:

string_new.1.mat
string_new.2.mat
string_new.3.mat
string_new.4.mat
string_new.5.mat
string_new.6.mat
string_new.7.mat
string_new.8.mat
string_new.9.mat
string_new.10.mat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...