Splitting Cells Within A Cell Array

21 views (last 30 days)
Kyle Senczyszyn
Kyle Senczyszyn on 24 Apr 2022
Commented: Voss on 24 Apr 2022
Hello I am new to matlab and coding. I have a 225x1 cell array within matlab that contains a 1x2 cell within each cell. The players name and their coresponding toughdown stat are displayed within each 1x2 cell. I would like to convert the cell array so that it displays the name and number in two seperate columns. Any help is appreciated, thank you.
Here is the table I want to seperate:
Here is the function I used to loop through multiple excel files to gather the data:
function [DataArr] = CumulativeTDs(DataArr)
num_files = length(DataArr);
player_td = {};
for file = 1:num_files
table = readtable(DataArr{file});
rows = height(table);
for row = 1:rows
player = string(table2array(table(row,1)));
td = string(table2array(table(row,12)));
num_players = length(player_td);
check = false;
for i = 1:num_players
if string(player_td{i}{1}) == player
player_td{i}{2} = player_td{i}{2} + str2double(td);
check = true;
end
end
if num_players == 0
player_td{1} = {player{1}, str2double(td)};
elseif check == false
player_td{i+1} = {player{1}, str2double(td)};
end
end
end
[DataArr] = player_td;
end

Accepted Answer

Voss
Voss on 24 Apr 2022
% some made-up data in the same format as yours:
C = { {'Droo Breeze',157}; {'Thom Braadie',751}; {'Jo Mountaine',999}; {'Jon El-Weigh',666} }
C = 4×1 cell array
{1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell}
% do the format conversion:
C = vertcat(C{:})
C = 4×2 cell array
{'Droo Breeze' } {[157]} {'Thom Braadie'} {[751]} {'Jo Mountaine'} {[999]} {'Jon El-Weigh'} {[666]}
It's likely that you can modify the code that reads the files, in order to get the result into this format in the first place and not need to do this conversion.
  2 Comments
Kyle Senczyszyn
Kyle Senczyszyn on 24 Apr 2022
Thank you! I am sure there is a better way to do it but this will work for my needs.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 24 Apr 2022
Edited: Matt J on 24 Apr 2022
player=cellfun(@(c) c{1},C,'uni',0);
stats=cellfun(@(c) c{2},C,'uni',0);
T=table(string(player(:)),stats(:),'Var','Player','Stat')

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!