0

I would like to split a cell array of strings and take the first output argument like the following

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

Is there a way to do the operation in one line, a.k.a specify argument 1 in the cellfun call?

Marouen
  • 907
  • 3
  • 13
  • 36
  • I think you might be able to do this with `subsref`, as in [Subsref with cells](http://stackoverflow.com/q/18384735/5358968) – Steve Apr 20 '17 at 12:49

2 Answers2

2

One Line Solution

you can use:

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

Result

ans = 
a
b
c
ibezito
  • 5,782
  • 2
  • 22
  • 46
1

Another option would be to use regexp:

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

Output:

result =

  3×1 cell array

    'a'
    'b'
    'c'
souty
  • 607
  • 3
  • 10