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:

 채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 25일

0 개 추천

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

NA
NA 2020년 10월 26일
편집: NA 2020년 10월 26일
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 2020년 10월 26일
Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

태그

질문:

NA
2020년 10월 25일

댓글:

NA
2020년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by