Can´t declare strings - Doublequotes are invalid characters

조회 수: 10 (최근 30일)
Patricio Whittingslow
Patricio Whittingslow 2017년 4월 18일
댓글: Image Analyst 2017년 4월 18일
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
Stephen23
Stephen23 2017년 4월 18일
편집: Stephen23 2017년 4월 18일
The ability to defines string types using double quotes was added in R2017a:
Always use the help for your installed MATLAB, not the online help (which is for the most recent version).

댓글을 달려면 로그인하십시오.

채택된 답변

Honglei Chen
Honglei Chen 2017년 4월 18일
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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 4월 18일
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
Stephen23
Stephen23 2017년 4월 18일
편집: Stephen23 2017년 4월 18일
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
Image Analyst 2017년 4월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by