extracting first letter from the cell

hello,
I have two cell arrays: nam1 = {'John', 'Adam', 'Emma'} nam2 = {'Doe', 'Willson', 'Brown'}
I want to create one array with initials, like this: init = {'JD', 'AW', EB'}
I tried extracting first letters from each array like this:
if true
% code
end
ininam1 = cellfun (@(x) x(1),nam1,'un',0)
ininam2 = cellfun (@(x) x(1),nam2,'un',0)
but this already fails (Index exceeds array bounds). Can you help me with this? Also combining these two arrays together?
Thanks in advance!
K.

2 件のコメント

Stephen23
Stephen23 2018 年 10 月 15 日
It worked fine when I tried it:
>> nam1 = {'John', 'Adam', 'Emma'};
>> nam2 = {'Doe', 'Willson', 'Brown'};
>> ininam1 = cellfun (@(x) x(1),nam1,'un',0)
ininam1 = 'J' 'A' 'E'
>> ininam2 = cellfun (@(x) x(1),nam2,'un',0)
ininam2 = 'D' 'W' 'B'
Kyle Pastor
Kyle Pastor 2020 年 4 月 9 日
編集済み: Kyle Pastor 2020 年 4 月 9 日
I had the same issue. Turns out one of the elements of my cell arrays was an empty string.
A = {'Works','OK_DOKIE',''};
element 3 will cause the fail!
-K

サインインしてコメントする。

 採用された回答

Kevin Chng
Kevin Chng 2018 年 10 月 15 日
編集済み: Kevin Chng 2018 年 10 月 15 日

0 投票

Hi,

nam1 = {'John', 'Adam', 'Emma'} ;
nam2 = {'Doe', 'Willson', 'Brown'};
ininam1 = strcat(nam1{1}(1),nam2{1}(1))

3 件のコメント

Kevin Chng
Kevin Chng 2018 年 10 月 15 日
編集済み: Kevin Chng 2018 年 10 月 15 日
or
ininam1 = cellfun (@(x) x(1),nam1,'un',0)
ininam2 = cellfun (@(x) x(1),nam2,'un',0)
name=strcat(ininam1,ininam2)
KDRA
KDRA 2018 年 10 月 15 日
This gives me only 'JD' and the rest disappears..
Kevin Chng
Kevin Chng 2018 年 10 月 15 日
It will return all the combination in name
nam1 = {'John', 'Adam', 'Emma'} ;
nam2 = {'Doe', 'Willson', 'Brown'};
ininam1 = cellfun (@(x) x(1),nam1,'un',0);
ininam2 = cellfun (@(x) x(1),nam2,'un',0);
name=strcat(ininam1,ininam2);

サインインしてコメントする。

その他の回答 (1 件)

Adam
Adam 2018 年 10 月 15 日

0 投票

res = cellfun( @(x,y) [x(1) y(1)], nam1, nam2, 'UniformOutput', false' )
works fine for me on your example, but then so does your own code. Do you have an actual example where it leads to an error?

3 件のコメント

KDRA
KDRA 2018 年 10 月 15 日
while using this solution I get an error 'All of the input arguments must be of the same size and shape. Previous inputs had size 3 in dimension 1. Input #3 has size 1'. I am actually accessing a structure in the loop, so maybe that's the reason why it flips?
if true
% code
end
nam1 = cell(mfiles,1);
for gg = 1:mfiles
nam1{gg} = s{gg}.SaData.ClientInfo.FirstName.Text;
nam2{gg} = s{gg}.SaData.ClientInfo.LastName.Text;
end
res = cellfun( @(x,y) [x(1) y(1)], nam1, nam2, 'UniformOutput', false' )
Adam
Adam 2018 年 10 月 15 日
編集済み: Adam 2018 年 10 月 15 日
Well, the problem certainly appears to be with your inputs, but I don't know from that exactly what the inputs look like. The example you gave works fine and I would imagine any pair of cell arrays of equal length, each of which have names of at least 1 letter in them would work. If you have empty cells or if nam1 and nam2 are not the same length then it won't work.
What do nam1 and nam2 look like in a real case that fails? Giving a simplified example in a question is useful, but only if it replicates the problem you are having for the question you ask!
KDRA
KDRA 2018 年 10 月 15 日
I literally have 3 names and 3 family names, I just changed them to fake names. They indeed have the same length and they are not empty so in a nutshell, is exactly the same as my simple example.
with this:
if true
% code
end
ininam1 = cellfun (@(x) x(1:1),nam1,'un',0);
ininam2 = (cellfun (@(x) x(1:1),nam2,'un',0)).';
I managed to extract first letters from both arrays and combine tohether with this line: name=strcat(ininam1,ininam2); Still not sure why your solution did not work...

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2018 年 10 月 15 日

編集済み:

2020 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by