String Escape
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
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0.0 |