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

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 8 Mär. 2013

0 Stimmen

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

Weitere Antworten (1)

per isakson
per isakson am 8 Mär. 2013

0 Stimmen

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

1 Kommentar

matthew arnott
matthew arnott am 11 Mär. 2013
i think both answers are good - i didnt realise you could only accept one - many thanks to both.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by