view
view

Reputation: 555

Difficulty in creating filename

Hey guys. I have some difficulty in creating a filename. okay, here is what I want to do: a matlab function called file_save(filename,input_data) is to save data into a xml file. so in a for loop, I want to create xml file with sequential filename eg. output1.xml output2.xml output3.xml

I guess there are some way of combining filename? Can anybody give me some help?

Thanks!

Upvotes: 2

Views: 256

Answers (1)

Doresoom
Doresoom

Reputation: 7458

You can concatenate strings the same way as arrays in MATLAB. (Actually, strings are treated like character arrays.)

For file #n,

name='MyFile';
ext='.xml';
filename=[name,num2str(n),ext];

should get you what you want.

As @Andrew points out in the comments, you can also use sprintf to format the filename:

filename = sprintf('MyFile%0*d.xml', ceil(log10(N+1)), n);

where N is the total number of files you plan on naming, and n is your current iteration. The ceil(log10(N+1)) gets you the number of digits you need for correct leading zero-padding.

@Azim points out that num2str can accomplish the same thing:

filename=[name,num2str(n,['%0' num2str(ceil(log10(N+1))),'d']),ext];

Upvotes: 2

Related Questions