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

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일

0 개 추천

A loop is fine for something like that.

댓글 수: 2

The problem is I have hundreds of cells and each one has a different length and I want to put numbers like showed above.
Your problem is not a problem. Even if you have 5000 cells, it will still be pretty fast.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2013년 10월 7일

댓글:

2013년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by