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 combinin开发者_如何学Gog filename? Can anybody give me some help?
Thanks!
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];
精彩评论