Can´t declare strings - Doublequotes are invalid characters
33 views (last 30 days)
Show older comments
Patricio Whittingslow
on 18 Apr 2017
Commented: Image Analyst
on 18 Apr 2017
So I´ve been trying to use the computers at my university and the ones in a specific lab have the same problem. When I try to declare a string variable like so:
>>stringy="abc123alleyesonme"
stringy="abc123alleyesonme"
↑
Error: The input character is not valid in MATLAB statements or expressions.
I´ve tried copy pasting doublequotes from the internet, it seems that Matlab does not accept them, or at least in this weird configuration.
Version : Matlab R2016a
1 Comment
Accepted Answer
Honglei Chen
on 18 Apr 2017
According to release notes, declaring string using double quotes is introduced in R2017a, so you will not be able to use it in R2016a.
See the language and programming part.
HTH
0 Comments
More Answers (1)
Image Analyst
on 18 Apr 2017
Like the others said, the Python-like ability to use either kind of quotes was added in R2017a. Since you have an old version, you need to continue to use single quotes. This works fine:
stringy = 'abc123alleyesonme'
2 Comments
Stephen23
on 18 Apr 2017
Edited: Stephen23
on 18 Apr 2017
I would not describe this as a "Python-like ability", because in Python double and single quotes both create variables of exactly the same type (either str or unicode). In MATLAB the creation of string arrays using double quotes is quite different to creating character arrays using single quotes, and is thus not very "like" Python.
Image Analyst
on 18 Apr 2017
Thanks for pointing that out. In R2017a if we run this:
a = 'abc'
whos a
b = "bcd"
whos b
we see
a =
'abc'
Name Size Bytes Class Attributes
a 1x3 6 char
b =
"bcd"
Name Size Bytes Class Attributes
b 1x1 134 string
Of course in R2016b and earlier you were only able to do the first "a" assignment. But it's good to know that they are actually different types of character strings, and that the double quote one apparently has a ton of overhead (more than 20 times as many bytes for this example).
And they're not interchangeable, for example, try passing a dobule quote one into a function like dir:
a = 'test2.m'
dir(a)
b = "test2.m"
dir(b)
Single quotes are fine. Double quotes are not:
a =
'test2.m'
test2.m
b =
"test2.m"
Error using dir
Function is not defined for 'string' inputs.
Error in test2 (line 13)
dir(b)
I bet that will trip up a lot of people in the future.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!