Special characters not translated
20 views (last 30 days)
Show older comments
Dear all,
I am pretty new to Matlab and since I did some programming before this question really feels stupid. I try to include special characters in a string which does not work at all:
txt='test \t string';
disp(txt);
The result is always:
test \t string
The same ist true for \n etc., as Matlab does not understand I like to have a tabulator in the string. What am I doing wrong?
Best regards
Sebastian
3 Comments
Stephen23
on 27 Nov 2018
Edited: Stephen23
on 28 Nov 2018
"It is true, that Matlab does not state anywhere, that it works that way, but also I could not find anywhere that it works the other way."
It states in the documentation that "Create a character vector by enclosing a sequence of characters in single quotation marks": And that is exactly what you are doing: you told MATLAB to put '\' and 't' into a character vector, and so that is what it did, just as the documenation states. It does not state that these will magically be turned into something else (most documentation focuses on what something does rather than what it does not do (which after all is an infinitely long list)).
"That a \t is sometimes (depending on the function) is interpreted as a tab and sometimes just as \t is a remarkable inconsistency"
The string creation itself is perfectly consistent. In all cases when you create a string/character vector, the characters are literally interpreted (except for double/single quotes respectively), so '\t' defines two characters in that string/char vector: a '\' followed by a 't'.
Some functions will identify special combinations of characters and ascribe to them some special meaning. So it is only when your string/char vector is parsed by some function (e.g. textscan, sprintf, etc) that your special character combinations might be given some special meaning. That depends on the function.
Note that other languages (intentionally) provide major inconsistencies in string definitions. For example, If you were to paste this into the middle of an existing longer string in a Python script, do you know exactly how these character would be interpreted?
abc\t"def"ghi
The answer is "no", because how the characters are interpreted in Python depends on how the longer string itself was defined: single quotes, double quotes, duobled, tripled, raw string literal, etc. Simply looking at those characters gives you no idea how they will be interpreted, until you have a whole lot of context.
Answers (2)
Sebastian Sommer
on 27 Nov 2018
1 Comment
Stephen23
on 27 Nov 2018
Edited: Stephen23
on 27 Nov 2018
"How do I know if a command understands special characters?"
By reading their documentation.
"Is there a general rule?"
Not really. It is important to note that for functions which accept \t, \n as inputs (e.g. textscan) those inputs are not converted to a tab or a newline character when you define the string/char vector like that: the string/char vector contains a backslash followed by t or n. It depends solely on the function to handle that input string/char vector in a special way, so the only way to know if a function processes some particular character combination in a special way is to read its help.
So I guess the general rule is "read the documentation".
"My original purpose was to find tabs in a text file, which I read line by line. How do I do that?"
Use regexp (which does handle \t quite happily).
See Also
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!