Special characters not translated

20 views (last 30 days)
Sebastian Sommer
Sebastian Sommer on 27 Nov 2018
Edited: Stephen23 on 28 Nov 2018
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
Sebastian Sommer
Sebastian Sommer on 27 Nov 2018
Edited: Sebastian Sommer on 27 Nov 2018
Well, yes and no.
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.
That a \t is sometimes (depending on the function) is interpreted as a tab and sometimes just as \t is a remarkable inconsistency. I can't not remember any programming language on top off my head which behaves this way (I can think of Basic, Pascal, C/C++, LabView and also HTML and LaTex; I might be wrong, though). Since Matlab is pretty new to me I assumed it worked similar as any other lanuage I know.
To just say, I should read the documentation does not really help, because it is sometimes hard to find the the exact topic; that is why I posted here. I didn't even know regexp existed, before I saw yout answer; so what should I have looked for?
Ok, but know I know how it is done. Thanks.
Stephen23
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.

Sign in to comment.

Answers (2)

madhan ravi
madhan ravi on 27 Nov 2018
Edited: madhan ravi on 27 Nov 2018
Read about sprintf() it does exactly what you want.
sprintf('test \t string')

Sebastian Sommer
Sebastian Sommer on 27 Nov 2018
Yes, that works. Thank you!
However, this leads to some follow up questions:
  • How do I know if a command understands special characters? Is there a general rule? There seems to be some redundant syntaxes for many tasks.
  • My original purpose was to find tabs in a text file, which I read line by line. How do I do that? Neither
strfind(textLine,'\t');
nor
contains(textLine,'\t');
seems to work.
  1 Comment
Stephen23
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).

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!