0

I have a problem removing the text and special characters from the string. For eg:
str = 'Accleration [ms^{{-}2}]';

The expected output: str_out = 'Acceleration'; I tried using the function regexprep but couldn't get the result as expected.

nik
  • 143
  • 4
  • 14

1 Answers1

1

You can try

opens = str == '[';
closes = str == ']';
nestingcount = cumsum(opens - [0 closes(1:end-1)]);
outstr = str(nestingcount == 0);

Note that trimming trailing spaces was not part of your specification, you'll have to do that as well to get your example to work right.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720