Scanning strings in a string array

조회 수: 9 (최근 30일)
Viviana Arrigoni
Viviana Arrigoni 2017년 10월 9일
편집: Guillaume 2017년 10월 9일
I see that if I declare a string between ' ', I can have access to each single character. For instance:
>> x = 'abc';
>> x(1)
ans = 'a'
While if I declare a string between " ", this doesn't happen:
>> x = "abc";
>> x(1)
ans = "abc"
Now, I need to scan an array of strings, the problem is that if the strings are between "", for example:
A = ["abc", "def", "ghi", "lmn"];
then it results length(A) = 4 (as I want it to be), but then if x = A(1) (i.e. x = "abc"), it is not possible to access to each character of x, and I need to modify such strings, so I need access to x's characters. On the other hand, if I declare the array as follows:
A = ['abc', 'def', 'ghi', 'lmn'];
it results that length(A) = 12 (I want it to be 4), and A(1) = 'a', and not 'abc' as I would like it to be! How do I overcome to this?

답변 (2개)

Stalin Samuel
Stalin Samuel 2017년 10월 9일
A = {'abc', 'def', 'ghi', 'lmn'};
A{1}
ans = 'abc'
A{1}(1)
ans = 'a'

Guillaume
Guillaume 2017년 10월 9일
편집: Guillaume 2017년 10월 9일
This is fully explained in the documentation. You use curly braces to access individual strings as char vector and then use normal indexing to access individual characters:
A = ["abc", "def", "ghi", "lmn"];
A{1}(1) %returns 'a'
If starting from your x, you still need to use the curly braces:
A = ["abc", "def", "ghi", "lmn"];
x = A(1);
x{1}(2) %returns 'b'
I'll admit it's a bit awkward.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by