MATLAB: Remove “( )” from string words

regexprepremove

Best Answer

regexp is very compact and powerful - however it can be a bit slow.
If speed is an issue a simple for loop and strrep can sometimes be faster:
db = { 'NEW' 'May' '(AFP)' 'US' 'prosecutors' 'on' };
pattern = '\(|)';
tic
newdb = regexprep(db, pattern, '');
toc
tic
loopDB = db;
for ii=1:length(db)
loopDB{ii} = strrep ( db{ii}, '(', '' );
loopDB{ii} = strrep ( loopDB{ii}, ')', '' );
end
toc
isequal ( newdb, loopDB )
Elapsed time is 0.002665 seconds.
Elapsed time is 0.000087 seconds.
ans =
1