How to convert all elements of a cell array to one string matrix.

6 次查看(过去 30 天)
Imagine this Example:
A = {'Steve';'wonder';'Marcus';'Jacob'};
and i want to convert to
B =['1-Steve 2-wonder 3-marcus 4-Jacob'];
I'm doing that inside of an loop!
What is the best way to solve that? Thanks

采纳的回答

Sean de Wolski
Sean de Wolski 2013-10-7
A = {'Steve';'wonder';'Marcus';'Jacob'};
B = strjoin(strcat(cellstr(num2str((1:numel(A)).')),'-',A).',' ')
13a or newer for strjoin

更多回答(1 个)

Matt J
Matt J 2013-10-7
编辑:Matt J 2013-10-7
A loop is fine for something like that.
  2 个评论
Alan
Alan 2013-10-7
The problem is I have hundreds of cells and each one has a different length and I want to put numbers like showed above.
Matt J
Matt J 2013-10-7
Your problem is not a problem. Even if you have 5000 cells, it will still be pretty fast.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by