user8396969
user8396969

Reputation:

In MATLAB, what is the difference between single quote and double quote?

For example, if I want to get an integer from user, and use input function:

prompt = "please input the sigma value for Gaussian filtering"
sigma = input(prompt)

will give an error message

while

prompt = 'Please input the sigma value for Gaussian filtering'
sigma = input(prompt)

will successfully run and get the user input.

My question is, what is the difference between "" and ''?

Thank you.

Upvotes: 2

Views: 7389

Answers (2)

Oskar Limka
Oskar Limka

Reputation: 143

While character arrays and strings are interchangeable in many ways, there are some differences.

Here is my favorite example for students:

>> a = ['1','2']

vs

>> b = ["1","2"]

The resulting a is the character array '12' while b is a 1x2 string array with entries "1" and "2".

A second, hopefully even more illuminating example, is

>> d = sprintf("%1.2e1",3.4)
>> e = sprintf('%1.2e1',3.4)
>> d(1)
>> e(1)

Notice how both styles can be used in string manipulation functions such as sprintf, but the resulting arrays are 1x1 for d and 1xn for e

Upvotes: 4

ThP
ThP

Reputation: 2342

The single quote is used to define a character array.
Starting from version R2017a, double quote is used to define a String. From what I remember, it can’t be used at all before that version.
For more information you can look here.

Upvotes: 3

Related Questions