我想在Matlab中索引一个表达式,只返回第一个参数
new_mets{i}=strsplit(mets{j},' (');
ans =
'Anteisopentadecanoylcardiolipin' 'B. subtilis)'
像这样:
new_mets{i}=strsplit(mets{j},' (')(1);
ans =
'Anteisopentadecanoylcardiolipin'
但我明白了:
Error: ()-indexing must appear last in an index expression
当然我可以先将它保存为变量,然后将其保存为索引,但这样效率低,肯定会有更简单的方法
答案 0 :(得分:2)
您可以使用正则表达式仅获取第一部分:
new_mets{i} = regexp(mets{j}, '^.+(?= \()', 'match');
示例:
>> regexp('aaa (bb)', '^.+(?= \()', 'match')
ans =
'aaa'
另一种方法:
ind = strfind(mets{j}, ' ('); %// find starting indices of matches
new_mets{i} = mets{1}(1:ind(1)-1); %// take substring previous to first match