separate strings that are inside a cell

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 日
Please post a small example, which clarifies, what you want to achieve. Deleting and keeping the messages sounds like a contradiction.
flashpode
flashpode 2021 年 9 月 17 日
In the image you can see that in the position 9 there is a string that has 2 messages. The same in the position 20 and 21.
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 日

0 投票

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 件のコメント

flashpode
flashpode 2021 年 9 月 18 日
That's it. It worked

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2021 年 9 月 17 日
編集済み: Adam Danz 2021 年 9 月 17 日

1 投票

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 件のコメント

flashpode
flashpode 2021 年 9 月 18 日
what I want is to separate the line 8 for exemple into three:
ee
vse
xxx
Put each message that you got in this code:
[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" ]}
in a different line
flashpode
flashpode 2021 年 9 月 18 日
The code you gave me just transpose the strings. But I guess you can join the strings into one and then split into two lines, no?
flashpode
flashpode 2021 年 9 月 18 日
I have done some research and found the function strsplit but it gives me the error using it. I put this:
I guess I could join the cells and then split them with a delimeter.
D = strsplit(msg_match,'!AIV');
Jan
Jan 2021 年 9 月 18 日
編集済み: Jan 2021 年 9 月 18 日
It would be easy to help you, if you provide an example with an output for a specific input. I ask for this repeatedly, but you show us screen shots and rough explanations only. "get the messages from inside like the other lines" is not clear enough and an image showing "2x1 string" does not help also.
"join the strings into one and then split into two lines" - why do you want to join them before splitting? This sounds, like you have a specific purpose for thi, but I cannot guess this detail.
"found the function strsplit but it gives me the error using it" - post your code an a complete copy of the error message. Remember, that the readers in the forum have no idea of what you are doing, but this is required to assist you.
Adam did you the favour to create an example for the input:
C = {"sdfsd"; "dare"; ["abs";"ses"]; "erwe"; "serwe"; ...
["444";"wer"]; "adrwed"; ["ee";"vse";"xxx"]; "sered"}
Now it is your turn to show us, what you want to get. Post it as code, which produces the wanted output, not as description like "in a different line".
flashpode
flashpode 2021 年 9 月 18 日
Okay so, what I have is:
C = 9×1 cell array
{["sdfsd" ]}
{["dare" ]}
{["abs" "ses" ]}
{["erwe" ]}
{["serwe" ]}
{["444" "wer" ]}
{["adrwed" ]}
{["ee" "vse" "xxx"]}
{["sered" ]}
and what I want is
C = 13×1 cell array
{["sdfsd" ]}
{["dare" ]}
{["abs" ]}
{["ses" ]}
{["erwe" ]}
{["serwe" ]}
{["444" ]}
{["wer" ]}
{["adrwed" ]}
{["ee" ]}
{["vse" ]}
{["xxx" ]}
{["sered" ]}
Adam Danz
Adam Danz 2021 年 9 月 18 日
The mini lesson hopefully learned here is to provide a clear question with examples to illustrate the problem clearly.
flashpode
flashpode 2021 年 9 月 18 日
Yeah I am sorry
Jan
Jan 2021 年 9 月 18 日
It's alright. Asking questions is not trivial. It is the nature of problems, that the asking person is not aware of which information is required for a solution. I think, you have learned how to solve this problem and something about keeping the fokus in the question.
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.
flashpode
flashpode 2021 年 9 月 18 日
Okay, Thank you for your time

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2021 年 9 月 17 日

コメント済み:

2021 年 9 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by