Split a cell that contains a string

11 ビュー (過去 30 日間)
Melissa Ette
Melissa Ette 2020 年 10 月 25 日
コメント済み: Melissa Ette 2020 年 10 月 26 日
I have a cell u(38,1) each row has string looking like this :
for example u{1,1} = ''198823 765389 white''
u{2,1} = ''198426 725312 black''
I want the cell u to be divided in 3 colomns such as
u{1,1} = ''198823'' u{1,2}= ''765389'' u{1,3}=''white''
u{2,1} = ''198426'' u{2,1}= ''725312'' u{2,3}=''black''
so basically I want my u(38,1) to become u(38,3)
I have tried strplit, but it seems like it won't work with a cell,
strplit(u,',') %doesn't work'
strplit(u{1,1},',') %works on the first line and returns a cell (1,3)

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 10 月 26 日
Look into cellfun. This will apply a function to each cell in the cell array.
Here's an example of one way of doing this.
u{1,1} = "198823 765389 white";
u{2,1} = "198426 725312 black"
u = 2x1 cell array
{["198823 765389 white"]} {["198426 725312 black"]}
a=cellfun(@strsplit,u,'UniformOutput',false);
a=vertcat(a{:})
a = 2×3 string array
"198823" "765389" "white" "198426" "725312" "black"
  2 件のコメント
Cris LaPierre
Cris LaPierre 2020 年 10 月 26 日
編集済み: Cris LaPierre 2020 年 10 月 26 日
Actually, why keep this a cell? This can be done much simpler if you use strings.
u{1,1} = "198823 765389 white";
u{2,1} = "198426 725312 black"
u = 2x1 cell array
{["198823 765389 white"]} {["198426 725312 black"]}
% Convert cell array to a string array.
u2 = string(u)
u2 = 2×1 string array
"198823 765389 white" "198426 725312 black"
a2 = split(u2)
a2 = 2×3 string array
"198823" "765389" "white" "198426" "725312" "black"
Melissa Ette
Melissa Ette 2020 年 10 月 26 日
Thank you it worked with a string array!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by