MATLAB Answers

Split comma seperated values inside a cell in to multiple columns

2 views (last 30 days)
Hari krishnan
Hari krishnan on 11 Aug 2021
Edited: Stephen on 11 Aug 2021
I have a cell which has comma seperated values in each rows. The size of each row is different. I want to split the each value in the row to a seperate column. Any suggestion will be appreciated. A sample cell is attached.

Accepted Answer

Rik
Rik on 11 Aug 2021
S=load(websave('split_mat.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/709142/split_mat.mat'));
split_mat=S.split_mat;
x=cellfun(@(x)str2double(split(x,',')).',split_mat,'UniformOutput',false);
format('compact'),celldisp(x)
x{1} = 1 x{2} = 2 x{3} = 1 3 x{4} = 2 4 x{5} = 3 5 x{6} = 4 6 x{7} = 1 1 1 5 7 x{8} = 1 2 2 2 6 8 x{9} = 2 3 3 3 7 9 x{10} = 3 4 4 4 8 10 x{11} = 4 5 5 5 9 11 x{12} = 5 6 6 6 10 12 x{13} = 6 7 7 7 11 13 x{14} = 1 1 1 9 10 10 10 14 x{15} = 2 2 2 10 11 11 11 15 x{16} = 3 3 3 11 12 12 12 16 x{17} = 4 4 4 12 13 13 13 17 x{18} = 5 5 5 13 14 14 14 18 x{19} = 6 6 6 14 15 15 15 19 x{20} = 7 7 7 15 16 16 16 20 x{21} = 8 8 8 16 17 17 17 21 x{22} = 9 9 9 17 18 18 18 22 x{23} = 10 10 10 18 19 19 19 23 x{24} = 11 11 11 19 20 20 20 24 x{25} = 12 12 12 20 21 21 21 25 x{26} = 13 13 13 21 22 22 22 26 x{27} = 14 14 14 22 23 23 23 27 x{28} = 15 15 15 23 24 24 24 28 x{29} = 16 16 16 24 25 25 25 29 x{30} = 17 17 17 25 26 26 26 30 x{31} = 1 18 18 18 26 27 27 27 31 x{32} = 2 19 19 19 27 28 28 28 32 x{33} = 3 20 20 20 28 29 29 29 33 x{34} = 4 21 21 21 29 30 30 30 34 x{35} = 1 5 22 22 22 30 31 31 31 35 x{36} = 2 6 23 23 23 31 32 32 32 36 x{37} = 3 7 24 24 24 32 33 33 33 37 x{38} = 4 8 25 25 25 33 34 34 34 38 x{39} = 1 5 9 26 26 26 34 35 35 35 39 x{40} = 2 6 10 27 27 27 35 36 36 36 40 x{41} = 3 7 11 28 28 28 36 37 37 37 41 x{42} = 1 4 8 12 29 29 29 37 38 38 38 42 x{43} = 2 5 9 13 30 30 30 38 39 39 39 43 x{44} = 3 6 10 14 31 31 31 39 40 40 40 44 x{45} = 4 7 11 15 32 32 32 40 41 41 41 45 x{46} = 5 8 12 16 33 33 33 41 42 42 42 46 x{47} = 6 9 13 17 34 34 34 42 43 43 43 47 x{48} = 7 10 14 18 35 35 35 43 44 44 44 48 x{49} = 8 11 15 19 36 36 36 44 45 45 45 49 x{50} = 9 12 16 20 37 37 37 45 46 46 46 50 x{51} = 10 13 17 21 38 38 38 46 47 47 47 51 x{52} = 11 14 18 22 39 39 39 47 48 48 48 52 x{53} = 12 15 19 23 40 40 40 48 49 49 49 53 x{54} = 13 16 20 24 41 41 41 49 50 50 50 54 x{55} = 14 17 21 25 42 42 42 50 51 51 51 55 x{56} = 15 18 22 26 43 43 43 51 52 52 52 56 x{57} = 16 19 23 27 44 44 44 52 53 53 53 57 x{58} = 17 20 24 28 45 45 45 53 54 54 54 58 x{59} = 18 21 25 29 46 46 46 54 55 55 55 59 x{60} = 19 22 26 30 47 47 47 55 56 56 56 60 x{61} = 20 23 27 31 48 48 48 56 57 57 57 61 x{62} = 21 24 28 32 49 49 49 57 58 58 58 62 x{63} = 22 25 29 33 50 50 50 58 59 59 59 63 x{64} = 23 26 30 34 51 51 51 59 60 60 60 64 x{65} = 24 27 31 35 52 52 52 60 61 61 61 65 x{66} = 25 28 32 36 53 53 53 61 62 62 62 66 x{67} = 26 29 33 37 54 54 54 62 63 63 63 67 x{68} = 27 30 34 38 55 55 55 63 64 64 64 68 x{69} = 28 31 35 39 56 56 56 64 65 65 65 69 x{70} = 29 32 36 40 57 57 57 65 66 66 66 70 x{71} = 30 33 37 41 58 58 58 66 67 67 67 71 x{72} = 31 34 38 42 59 59 59 67 68 68 68 72 x{73} = 32 35 39 43 60 60 60 68 69 69 69 73 x{74} = 33 36 40 44 61 61 61 69 70 70 70 74 x{75} = 34 37 41 45 62 62 62 70 71 71 71 75 x{76} = 35 38 42 46 63 63 63 71 72 72 72 76 x{77} = 36 39 43 47 64 64 64 72 73 73 73 77 x{78} = 37 40 44 48 65 65 65 73 74 74 74 78 x{79} = 38 41 45 49 66 66 66 74 75 75 75 79

More Answers (1)

Stephen
Stephen on 11 Aug 2021
Edited: Stephen on 11 Aug 2021
Simpler and more efficient:
% load your data:
S = load('split_mat.mat');
C = S.split_mat;
% convert to numeric:
F = @(t)sscanf(t,'%f,',[1,Inf]);
D = cellfun(F,C,'uni',0)
D = 79×1 cell array
{[ 1]} {[ 2]} {[ 1 3]} {[ 2 4]} {[ 3 5]} {[ 4 6]} {[ 1 1 1 5 7]} {[ 1 2 2 2 6 8]} {[ 2 3 3 3 7 9]} {[ 3 4 4 4 8 10]} {[ 4 5 5 5 9 11]} {[ 5 6 6 6 10 12]} {[ 6 7 7 7 11 13]} {[ 1 1 1 9 10 10 10 14]} {[2 2 2 10 11 11 11 15]} {[3 3 3 11 12 12 12 16]}

Community Treasure Hunt

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

Start Hunting!