How to convert a 1x1 cell to a string?

How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx

 Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 13 Nov 2024
Modificato: MathWorks Support Team il 13 Nov 2024

67 voti

Questo/a risposta è stato/a segnalato/a da Walter Roberson
To convert a cell array of character vectors to a character array, use the “char” function. A = {'line'} B = char(A) To extract the contents from a cell, index using curly braces. A = {'line'} B = A{1} Starting in R2016b, you can store text in string arrays. To convert a cell array to a string array, use the “string” function. A = {'line'} B = string(A) For more information, please refer to the MathWorks documentation on Cell Arrays of Character Vectors and Text in String and Charater Arrays.

6 Commenti

Powen Ru
Powen Ru il 8 Gen 2018
Modificato: Powen Ru il 8 Gen 2018
Thanks. It works.
If you are using a recent version of MATLAB you may find that a string or string array is more convenient then the character array created in the above example and by the char function:
>> string({'one'})
ans =
"one"
>> string({'one','two'})
ans =
1×2 string array
"one" "two"
it has done magic to me thanks a lot
Thanks Azzi Abdelmalek. Works perfect.
That's a piece of cake. Thanks a lot!
gracias

Accedi per commentare.

Più risposte (2)

Azzi showed you how to extract the string from a cell. Another way is to convert the cell with char():
ca={'line'} % This is our cell array.
str = char(ca) % Convert it to a character array (string).
Net, both give the same result, just different ways of getting there. If your cell array is only a single solitary cell, then you should not even use a cell in the first place - use a string.
Even if you have more than 1 string in our cell array, an easy way can be:
S = {'Hello',' ','world'}
ss = [S{:}]

5 Commenti

How to covert cell into string and delimited with space to store into matrix
Is this what you mean?
ca = {'Hello',' ','world'} % Cell Array.
charArray = [ca{:}]
str = convertCharsToStrings(charArray)
If not, start a new question and give your starting cell, and expected output.
Maybe you are looking for this:
S = {'Hello','World'};
s2 = cell(1,length(S)); s2(:)={' '};
ss = [S;s2];
ss = [ss{:}]
Just a point of clarification. ss is a character array while str is a string. A few versions ago, MATLAB made these different types of variables, though in common speech, people call either one "strings".
Right. Thanks.

Accedi per commentare.

Categorie

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by