-1

I want to index an expression in Matlab to return only the first argument

new_mets{i}=strsplit(mets{j},' (');
ans  = 
'Anteisopentadecanoylcardiolipin'    'B. subtilis)'

like this:

new_mets{i}=strsplit(mets{j},' (')(1);
ans  = 
'Anteisopentadecanoylcardiolipin'

but I get:

Error: ()-indexing must appear last in an index expression

of course I can save it as a variable first and index in subsequently, but that is inefficient and there must surely be an easier way

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • what is the content of `mets`? – m.s. Apr 14 '15 at 14:35
  • See answer in this post: http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it. – Ant Apr 14 '15 at 17:41

1 Answers1

2

You can use a regular expression to obtain only the first part:

new_mets{i} = regexp(mets{j}, '^.+(?= \()', 'match');

Example:

>> regexp('aaa (bb)', '^.+(?= \()', 'match')
ans = 
    'aaa'

Another approach:

ind = strfind(mets{j}, ' ('); %// find starting indices of matches
new_mets{i} = mets{1}(1:ind(1)-1); %// take substring previous to first match
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • if he does not want to use a temp variable in order to gain "efficiency", I doubt regexps will be accepted :) – m.s. Apr 14 '15 at 14:43
  • @m.s. Yes, I read regexps can be slow... I don't know, I don't use them very much myself – Luis Mendo Apr 14 '15 at 14:48