开发者

matlab: given a prefix string, how to generate a sequence

For example, the pref开发者_如何学JAVAix string is 'fig', I want to have a new string with the sequence 'fig1,fig2,fig3,...,fig100', how to do this conveniently without using a for loop? Many thanks!


I assume you want a cell array of strings, i.e. {'fig1','fig2',...'}

Here's one of many ways to achieve this (change the format string to 'fig%03i' if you want the output to be 'fig001','fig002' etc):

figString = arrayfun(@(x)sprintf('fig%i',x),1:100,'uniformOutput',false)

EDIT

If you only want a single string, i.e. 'fig1,fig2, ...', the easiest solution is to use sprintf:

figString = sprintf('fig%i,',1:100);
figString = figString(1:end-1); %# remove the comma at the end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜