Filter löschen
Filter löschen

Why array of strings are concatenated?

19 Ansichten (letzte 30 Tage)
Zeynab Mousavikhamene
Zeynab Mousavikhamene am 4 Nov. 2019
Kommentiert: Star Strider am 4 Nov. 2019
parameters=['K13','K24','K_52','K63','K74','K80','K101','K114','K162','MI','Ce','Ge','Pe'];
When I use:
size(parameters)
I get this:
ans =
1 39
But I wanted to get 1*13. I realized it concatenate all the strings to each other:
>> parameters
parameters =
'K13K24K_52K63K74K80K101K114K162MICeGePe'
How can I define it so that they become separated.
  2 Kommentare
Stephen23
Stephen23 am 4 Nov. 2019
Bearbeitet: Stephen23 am 4 Nov. 2019
Those are not strings, they are character vectors. Note the difference:
  • "..." scalar string
  • '...' character vector
String arrays and character arrays have very different properties and behaviors!
Star Strider
Star Strider am 4 Nov. 2019
Note that string arrays were introduced in R2016b. Prior to that, strings and character arrays were considered to be the same thing.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 4 Nov. 2019
The square brackets [] are a concatenation operator.
To keep the individual strings as strings, use a cell array:
parameters = {'K13','K24','K_52','K63','K74','K80','K101','K114','K162','MI','Ce','Ge','Pe'};
Note the curly brackets {} denoting a cell array.
See the documentation section on MATLAB Operators and Special Characters
  2 Kommentare
ME
ME am 4 Nov. 2019
This is the same suggestion I made five minutes before!
In fairness though you've actually explained the problem rather than just giving the solution as I did!
Star Strider
Star Strider am 4 Nov. 2019
@ME — Frequently, more than one person is in the process of writing an Answer to a particular Question at the same time. For more detailed Answers that require documentation references, it takes longer. If my Answer essentially duplicates a posted Answer when I discover it, I delete mine. If I believe it adds something, I leave it up.
I almost always delete my Answers if they are not Accepted.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

ME
ME am 4 Nov. 2019
You could just store this in a cell array instead - with one parameter inside each cell

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by