Filter löschen
Filter löschen

Different outputs when using { : }

4 Ansichten (letzte 30 Tage)
Daniya Zafar
Daniya Zafar am 7 Jan. 2022
Kommentiert: Kevin Holly am 7 Jan. 2022
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 Kommentar
Stephen23
Stephen23 am 7 Jan. 2022
Bearbeitet: Stephen23 am 7 Jan. 2022
"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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kevin Holly
Kevin Holly am 7 Jan. 2022
Bearbeitet: Kevin Holly am 7 Jan. 2022
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 Kommentare
Stephen23
Stephen23 am 7 Jan. 2022
Bearbeitet: Stephen23 am 7 Jan. 2022
@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 am 7 Jan. 2022
@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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by