开发者

Matlab: How can I put more than 2 strings of different length into one char element?

I would like to program a loop over a number of strings of different length. My problem is that I don't get my various strings stored in one element. I've tried the following:

string=['string1';'long开发者_高级运维erstring2']


%# Store
string = {'string1','longerstring2'};
%# Access
disp(string{1}); 

Output:

string1


You will need to store the strings in a cell array. The cellstr function may be useful to you for generating the cell array from a list of strings of equal length (padded with spaces), or you could just create the cell array manually.


The other solutions if you're happy with a cell array. Alternatively, you can do

char('string1','longerstring2')

which will pad with spaces, so is equivalent to (in this case):

['string1      ';'longerstring2']
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜