Hi,
I am new to MATLAB and have a very basic question.
Say I have a 2x2 string:
>> str = ["Jan" "Feb"; "Mar" "Apr"]
str =
2×2 string array
"Jan" "Feb"
"Mar" "Apr"
Why are the following two different?
str{:};
A = str{:};
Thanks in advance

댓글 수: 1

Stephen23
Stephen23 2022년 1월 7일
편집: Stephen23 2022년 1월 7일
"Why are the following two different?"
Because in one case you allocate to a variable, and in other you don't.
As an aside, using curly-braces on a string array creates a comma-separated list:
Note that the comma-separated list constitutes the content of the container array, i.e. character vectors and not string scalars.

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

 채택된 답변

Kevin Holly
Kevin Holly 2022년 1월 7일
편집: Kevin Holly 2022년 1월 7일

0 개 추천

When you used the braces the way you did, it turned the string array into a character array.
I would look at the following:

댓글 수: 3

Example:
String Array
str = "test"
str = "test"
Check size
size(str)
ans = 1×2
1 1
Look at first element, it is still a string array
str(1)
ans = "test"
Check size
size(str(1))
ans = 1×2
1 1
Now add braces, it is now a character array
str{1}
ans = 'test'
Check size
size(str{1})
ans = 1×2
1 4
Create Cell Array
{str}
ans = 1×1 cell array
{["test"]}
Let's create a character array
str = 'test'
str = 'test'
Check size
size(str)
ans = 1×2
1 4
Stephen23
Stephen23 2022년 1월 7일
편집: Stephen23 2022년 1월 7일
@Kevin Holly: so string arrays are really just cell arrays of character vectors?
Otherwise what is the relevance of the page "What is a Cell Array?" ?
Kevin Holly
Kevin Holly 2022년 1월 7일
@Stephen Cell arrays can also include numeric values and other arrays. When I first mentioned Daniya's use of braces caused the string array to become a character array, I didn't want Daniya to get confused and think the use of braces will always make a character array. For instance if you place braces around the variable, it creates a cell array. Also, it is good to know the differences. Then I decided to make the above comment to make things clear.

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2022년 1월 7일

댓글:

2022년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by