2017-04-20 62 views
0

第二個參數,我想分割字符串單元陣列,並採取先輸出參數,如以下如何選擇功能的cellfun

mycell={'a.1' 'b.2' 'c.3'}' 
result1 = cellfun(@(x) strsplit(x,'.'),mycell,'UniformOutput',false) 
result = cellfun(@(x) x{1},result1) 

有沒有辦法做到在一個操作線,又名在cellfun調用中指定參數1?

+0

我想你也許可以用'subsref'要做到這一點,如[的subsref與細胞(http://stackoverflow.com/q/18384735/5358968) – Steve

回答

1

另一種選擇是使用regexp

mycell = {'a.1' 'b.2' 'c.3'}'; 
result = regexp(mycell,'^[^.]+','match','once') 

輸出:

result = 

    3×1 cell array 

    'a' 
    'b' 
    'c' 
2

一個在線解決方案

你可以使用:

cellfun(@(x)subsref(strsplit(x,'.'),struct('type','{}','subs',{{1}})),mycell); 

結果

ans = 
a 
b 
c