String Escape

returns a string with special characters "escaped"
252 Downloads
Updated 16 Jul 2013

View License

function result = stresc(string)
returns a string with special characters "escaped"

i.e., the string "Randy's wonton\n % delicious!" becomes
"Randy''s wonton \\n %% delicious!".

Thus, the resulting string is suitable to be printed out and
interpreted by MATLAB. This function is especially useful if you are
writing code intended to produce code. Interpreted strings can be
freely placed into your code without worry of strings breaking code.

The following conversions are made:
% becomes %%
\ becomes \\
' becomes ''

Example Usage:
myString = inputdlg('Please enter a string you want printed',...
'Test StrEsc');
fprintf('%s\n', stresc(myString{1})); % original string, escaped
fprintf('fprintf(''%s'');\n', myString{1}); % won't work for all strings
fprintf('fprintf(''%s'');\n', stresc(myString{1}));

Cite As

Alexander Saites (2024). String Escape (https://www.mathworks.com/matlabcentral/fileexchange/42652-string-escape), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0