separate strings that are inside a cell

5 次查看(过去 30 天)
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?
  4 个评论
Jan
Jan 2021-9-17
Yes, I see it. What do you want todo with these strings? Remove them or replace them? In the latter case: by what?
flashpode
flashpode 2021-9-17
I want to get the messages from inside like the other lines

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-9-18
编辑:Jan 2021-9-18
Your description is not clear yet. I dare to guess:
% Input (thanks Adam):
C = {"sdfsd"; "dare"; ["abs";"ses"]; "erwe"; "serwe"; ...
["444";"wer"]; "adrwed"; ["ee";"vse";"xxx"]; "sered"}
% Wanted output:
C = {"sdfsd"; "dare"; "abs";"ses"; "erwe"; "serwe"; ...
"444"; "wer"; "adrwed"; "ee"; "vse"; "xxx"; "sered"}
% Solution:
D = cellstr(cat(1, C{:}))

更多回答(1 个)

Adam Danz
Adam Danz 2021-9-17
编辑:Adam Danz 2021-9-17
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"; ...
["444";"wer"]; "adrwed"; ["ee";"vse";"xxx"]; "sered"}
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" ]}
  10 个评论
Adam Danz
Adam Danz 2021-9-18
@vicente Noguer, no problem, I agree with Jan. Providing sample of inputs (as you provided) and samples of expected outputs goes a long way.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by