tabulate cell2table

I have a ‘group_belong’ like this:
The group_belong{2} is:
The group_belong{4} is:
I use the below code to remove empty cell and put the remain one vertically.
group_belong = group_belong(~cellfun('isempty',group_belong));
group_belong = vertcat(group_belong{:});
Then, I convert this cell array to table and put the variable name.
T = cell2table(group_belong,'VariableNames',{'A' 'B' 'C' 'D' 'E' 'I' 'R' 'T'});
But I got this result:
I do not know how to fix column 3 to this:

 Risposta accettata

Walter Roberson
Walter Roberson il 25 Ott 2020

0 voti

Your C variable in your table is probably already stored the way you want. It is the variable browser that is displaying separate columns, but T.C would be a 3 x 3 array [1 2 5;2 4 5; 2 4 5]
It is possible to get the kind of display you ask for out of the variable browser: it would show up like that if each row of T.C were a cell array. cell2table() does not create those kinds of entries automatically if all of the rows are the same size (like your column 3) but does if the rows are not all the same size (like your column 8).
I could suggest various ways to deliberately cause cells the same size to be stored as cells, but it strikes me that the easiest way might be to add an empty cell row onto the bottom of the input data, create the table, and then remove the last row.

3 Commenti

NA
NA il 26 Ott 2020
Modificato: NA il 26 Ott 2020
Thank you for your time.
I add this code
%% Add an empty cell row onto the bottom of the input data
size_each_cell = cellfun(@(m)size(m,2),group_belong(:,3));
group_belong_bk = group_belong(1,3);
if all(ismember(size_each_cell,size(cell2mat(group_belong(1,3)),2))) == 1
group_belong(end+1,:) = group_belong(end,:);
group_belong(end,:) = {[]};
end
Create the table
T = cell2table(group_belong,'VariableNames',{'A' 'B' 'C' 'D' 'E' 'I' 'R' 'T'});
Remove the empty row in a table
if all(ismember(size_each_cell,size(cell2mat(group_belong_bk),2))) == 1
idx = all(cellfun(@isempty,T{:,:}),2);
T(idx,:) = [];
end
But the problem is that, the column E, I, and R become cell. How do I avoid this?
If you know ahead of time which variables you want to become cells then when you do
group_belong(end,:) = {[]};
instead do
group_belong(end,[those column numbers]) = {[]};
NA
NA il 26 Ott 2020
Thank you.

Accedi per commentare.

Più risposte (0)

Richiesto:

NA
il 25 Ott 2020

Commentato:

NA
il 26 Ott 2020

Community Treasure Hunt

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

Start Hunting!

Translated by