필터 지우기
필터 지우기

Table function with cells

조회 수: 11 (최근 30일)
Pano
Pano 2023년 4월 27일
댓글: Pano 2023년 4월 27일
Hi,
I have a cell variable (166*1 cell) organized like this :
data_table =
1*4 cell
1*4 cell
1*4 cell
...
Each 1*4 cell is organized like this :
data_table{1,1} =
'A' '[1 25]' 'double' ""
data_table{2,1} =
'AF' '[1 1]' 'double' 1
...
When I use the command t = table(data_table{:,:}) (to export to an Excel file), all datas are reorganised in an unique row array :
'A' '[1 25]' 'double' "" 'AF' '[1 1]' 'double' 1....
How to keep the same arrangement as in the intial data_table array?
Thank you.
  댓글 수: 2
Stephen23
Stephen23 2023년 4월 27일
편집: Stephen23 2023년 4월 27일
Rather than storing lots of 1x4 cell arrays inside a 166x1 cell array, your data should probably be stored as one single 166x4 cell array. That would also make your task much easier. Lets try using some fake data:
data_table = cell(3,1);
data_table{1} = {'A', '[1 25]', 'double', ""};
data_table{2} = {'AF', '[1 1]', 'double', 1};
data_table{3} = {'B', '[0 0]', 'double', "Hi"};
Which probably shold be stored like this (or in a table):
C = vertcat(data_table{:})
C = 3×4 cell array
{'A' } {'[1 25]'} {'double'} {["" ]} {'AF'} {'[1 1]' } {'double'} {[ 1]} {'B' } {'[0 0]' } {'double'} {["Hi"]}
Which in turn makes saving the data easier:
writecell(C,'myfile.xlsx')
Do not use a loop to create lots of separate tables.
Pano
Pano 2023년 4월 27일
Thanks a lot!
writecell function doesn't exist in R2018b release.
I use following commands:
C = table(vertcat(data_table{:}))
writetable(C,'myfile.xlsx');

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

채택된 답변

Sufiyan
Sufiyan 2023년 4월 27일
Hi,
You can refer to the below code.
%sample data
data_table = cell(3,1);
data_table{1} = {'A', '[1 25]', 'double', ""};
data_table{2} = {'AF', '[1 1]', 'double', 1};
data_table{3} = {'B', '[0 0]', 'double', "Hi"};
% Convert each cell to a separate table
tables = cell(size(data_table));
for i = 1:numel(data_table)
tables{i} = cell2table(data_table{i});
end
% Concatenate tables
t = vertcat(tables{:});
disp(t);
Var1 Var2 Var3 Var4 ______ __________ __________ ____ {'A' } {'[1 25]'} {'double'} "" {'AF'} {'[1 1]' } {'double'} "1" {'B' } {'[0 0]' } {'double'} "Hi"
%writetable(t, 'filename.xlsx');
Hope this helps!
  댓글 수: 1
Pano
Pano 2023년 4월 27일
It works fine!
Thanks a lot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by