creating a list from another list

I have a list where not all the elements are the same length but they mostly are lets say my list is
mylist = {'ABCD', 'DEFG', 'HIJKL'} i want to return a new list mynewlist looks like {ABC, DEF, GHI} %%- its always the first 3 letters of the old list
many thanks probably seems a bit arbtirary but struggling

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 8일

0 개 추천

mylist = {'ABCD', 'DEFG', 'HIJKL'}
out=cellfun(@(x) x(1:3),mylist,'un',0)

추가 답변 (1개)

per isakson
per isakson 2013년 3월 8일

0 개 추천

>> mylist = {'ABCD', 'DEFG', 'HIJKL'}
mylist =
'ABCD' 'DEFG' 'HIJKL'
>> cellfun( @(str) str(1:3), mylist, 'uni', false )
ans =
'ABC' 'DEF' 'HIJ'
>>

댓글 수: 1

matthew arnott
matthew arnott 2013년 3월 11일
i think both answers are good - i didnt realise you could only accept one - many thanks to both.

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by