How to add \ before all special characters in MATLAB?

I am trying to add ‘\’ before all special characters in a string in MATLAB, could anyone please help me out. Here is the example:

tStr = 'Hi, I'm a Big (Not So Big) MATLAB addict; Since my school days!';

I want this string to be changed to:

'Hi\, I\'m a Big \(Not so Big \) MATLAB addict\; Since my school days\!'

Leave a Comment