How to convert a 1x1 cell to a string?

4.120 visualizaciones (últimos 30 días)
bh dhouha
bh dhouha el 1 de Feb. de 2015
Editada: MathWorks Support Team el 13 de Nov. de 2024 a las 3:58
How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Nov. de 2024 a las 0:00
Editada: MathWorks Support Team el 13 de Nov. de 2024 a las 3:58
Walter Roberson ha marcado con alerta este/a respuesta
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 comentarios
Junho Kweon
Junho Kweon el 25 de Abr. de 2019
That's a piece of cake. Thanks a lot!
Paula Camila
Paula Camila el 19 de Feb. de 2023
gracias

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 1 de Feb. de 2015
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.

Morteza Darvish Morshedi
Morteza Darvish Morshedi el 14 de Jun. de 2019
Even if you have more than 1 string in our cell array, an easy way can be:
S = {'Hello',' ','world'}
ss = [S{:}]
  5 comentarios
Image Analyst
Image Analyst el 15 de Feb. de 2021
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".
Morteza Darvish Morshedi
Morteza Darvish Morshedi el 20 de Feb. de 2021
Right. Thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by