フィルターのクリア

Can´t declare strings - Doublequotes are invalid characters

30 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by