Warning: Escape sequence ' ' is not valid. See 'help sprintf' for valid escape sequences
Show older comments
Im trying to create a function that changes the destination folder for some data put into a text file, trouble is that when I try to input an address such as J:\My_Documents\MATLAB into an inputdlg box, and then use sprintf on the answer so that it can be used in
filename = fullfile('Destination','Filename.txt');
sprintf thinks that the \'s in the destination name mean things like \n and \t so it gives me an error saying:
Warning: Escape sequence 'M' is not valid. See 'help sprintf' for valid escape sequences.
and sprintf itself only outputs 'J:' and not the whole destination name is there anyway I can I can change a written answer from an inputdlg box to a useful form for my purpose that doesn't try to do any unwanted formatting on it?
6 Comments
Star Strider
on 20 Mar 2014
You may need to provide more information. I can’t reproduce your problem (R2013b). This code:
Destination = inputdlg({'Destination'})
s = sprintf('%s', char(Destination))
produces this output for s:
s =
J:\My_Documents\MATLAB
with no errors.
Michael
on 20 Mar 2014
Star Strider
on 20 Mar 2014
My pleasure!
(I should have made that an Answer!)
Noam Greenboim
on 16 Sep 2014
Edited: Noam Greenboim
on 16 Sep 2014
I had a similar warning. look at your backslashes (\) maybe you forgot to add a valid letter after them (like \n, \t, etc.).
In this case, the backslash is a part of the string that you want to print. Use double backslash (\\) to print one backslash.
For example: look at the difference between:
fprintf('\nHello')
and
fprintf('\\nHello')
David Young
on 16 Sep 2014
Star Strider: I don't understand what sprintf is doing here. Isn't it the case that for any string s, sprintf('%s', s) is equal to s? So wouldn't
s = char(Destination);
be simpler? Have I missed something?
Guillaume
on 16 Sep 2014
Yes, sprintf('%s', s) doesn't do anything. Usually if you use the %s it's to insert a string into another.
For that matter, the char(s) is also useless, since the values returned by inputdlg are already strings, so
s = Destination;
Would work just as well and you might as well use Destination directly.
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!