Special characters not translated
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 27 de Nov. de 2018
"...as Matlab does not understand I like to have a tabulator in the string. What am I doing wrong?"
Nowhere in the MATLAB documentation does it state that what you are trying should work like that.
In MATLAB all characters used in a character array/string definitions are literal (except for the apostrophe ', which must be escaped). If you want to convert \t, \n, etc to tab, newline, etc., then you will need to use sprintf.
Sebastian Sommer
el 27 de Nov. de 2018
Editada: Sebastian Sommer
el 27 de Nov. de 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.
Respuestas (2)
madhan ravi
el 27 de Nov. de 2018
Editada: madhan ravi
el 27 de Nov. de 2018
sprintf('test \t string')
Sebastian Sommer
el 27 de Nov. de 2018
0 votos
1 comentario
"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).
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!