# separate strings that are inside a cell

4 views (last 30 days)
vicente Noguer on 17 Sep 2021
Commented: vicente Noguer on 18 Sep 2021
Hey so I got a cell that is 12000x1 and inside this cell I got some strings that are 2x1 or 3x1. I want to delete this string but get the messages from there and put them inside the cell. Is there any function that let me do this?
vicente Noguer on 17 Sep 2021
I want to get the messages from inside like the other lines

Jan on 18 Sep 2021
Edited: Jan on 18 Sep 2021
Your description is not clear yet. I dare to guess:
C = {"sdfsd"; "dare"; ["abs";"ses"]; "erwe"; "serwe"; ...
% Wanted output:
C = {"sdfsd"; "dare"; "abs";"ses"; "erwe"; "serwe"; ...
"444"; "wer"; "adrwed"; "ee"; "vse"; "xxx"; "sered"}
% Solution:
D = cellstr(cat(1, C{:}))
vicente Noguer on 18 Sep 2021
That's it. It worked

Adam Danz on 17 Sep 2021
Edited: Adam Danz on 17 Sep 2021
If your goal is to simply be able to read those lines, use cellfun to identify elements of cell array "C" that have more than one row of strings and then use cellfun again to transpose those rows.
Create demo data - you can see that there are 3 elements of C that are 2x1 or 3x1 string arrays
C = {"sdfsd"; "dare"; ["abs";"ses"]; "erwe"; "serwe"; ...
C = 9×1 cell array
{["sdfsd" ]} {["dare" ]} {2×1 string} {["erwe" ]} {["serwe" ]} {2×1 string} {["adrwed"]} {3×1 string} {["sered" ]}
Transpose mutli-rows
[nRows, ~] = cellfun(@size,C);
isMultiRow = nRows>1;
C(isMultiRow) = cellfun(@(a) {a'}, C(isMultiRow))
C = 9×1 cell array
{["sdfsd" ]} {["dare" ]} {["abs" "ses" ]} {["erwe" ]} {["serwe" ]} {["444" "wer" ]} {["adrwed" ]} {["ee" "vse" "xxx"]} {["sered" ]}
If you'd like to join those strings into 1, replace the line above with this line.
C(isMultiRow) = cellfun(@(a) {strjoin(a,' ')}, C(isMultiRow))
C = 9×1 cell array
{["sdfsd" ]} {["dare" ]} {["abs ses" ]} {["erwe" ]} {["serwe" ]} {["444 wer" ]} {["adrwed" ]} {["ee vse xxx"]} {["sered" ]}
vicente Noguer on 18 Sep 2021
Okay, Thank you for your time