how to convert a cell array to a string array

I have a cell array of 2048*1,each cell countains a string element, and i want to convert it into a string array (so that i could make it one hot coded)
how cloud i slove it?

2 Commenti

Stephen23
Stephen23 il 10 Gen 2022
Modificato: Stephen23 il 10 Gen 2022
"I have a cell array of 2048*1,each cell countains a string element..."
Highly unusual and not recommended: "Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages that come from using string arrays."
The best solution is to avoid creating this cell array in the first place, and instead store the data in a string array (or as a cell array of character vectors) right from the start. How was this cell array created?
The OP is valid. There could be situations where it is unavoidable. For example, reading in mixed data from an Excel spreadsheet using the readcell function.

Accedi per commentare.

Risposte (1)

S = {'Hello',' ','world',' ','i',' ','am',' ','here'}
ss = [S{:}]

4 Commenti

Those are character vectors, not strings:
S = {'Hello',' ','world',' ','i',' ','am',' ','here'}
S = 1×9 cell array
{'Hello'} {' '} {'world'} {' '} {'i'} {' '} {'am'} {' '} {'here'}
ss = [S{:}]
ss = 'Hello world i am here'
class(ss) % definitely not a string array
ans = 'char'
hi @Stephen what about this function?
str = convertCharsToStrings(chr)
Kelvin Prosyk
Kelvin Prosyk il 13 Mag 2023
Modificato: Kelvin Prosyk il 13 Mag 2023
Any valid answer to this question? OP wanted to convert to an array of strings, not a single vector of char. I have a similar problem.
S = {"Hello"," ","world"," ","i"," ","am"," ","here"}.'
S = 9×1 cell array
{["Hello"]} {[" " ]} {["world"]} {[" " ]} {["i" ]} {[" " ]} {["am" ]} {[" " ]} {["here" ]}
SA = reshape([S{:}], size(S))
SA = 9×1 string array
"Hello" " " "world" " " "i" " " "am" " " "here"

Accedi per commentare.

Categorie

Richiesto:

il 10 Gen 2022

Commentato:

il 13 Mag 2023

Community Treasure Hunt

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

Start Hunting!

Translated by