cell2table only using first cell array row

조회 수: 11 (최근 30일)
Gazebo
Gazebo 2018년 7월 30일
답변: Guillaume 2018년 7월 30일
I have a cell array as such:
E =
4x1 cell array
{1x4 cell}
{1x4 cell}
{1x4 cell}
{1x4 cell}
Where each 1x4 cell array consists of a char array.
E{1} =
{'Input'} {'azimuth_1p'} {'real'} {'-inf to inf'}
However the following code
T = cell2table(E{1,:},'VariableNames',{'IO_Type', 'Name', 'Type', 'Range'});
Only gives me the following table:
IO_Type Name Type Range
_______ ______________ ______ _____________
'Input' 'azimuth_1p' 'real' '-inf to inf'
Why aren't I getting all 4 rows in my table?

답변 (1개)

Guillaume
Guillaume 2018년 7월 30일
Why aren't I getting all 4 rows in my table?
Matlab does exactly what you asked. As you've shown E is a 4 (rows) x 1 (column) cell array. E{1, :} is the content of the 1st row and all the columns. There's only one column, so E{1, :} is the same as E{1, 1} or E{1}. The content of E{1} is a 1 (row) x 4 (column) cell array, hence you only get one row out of cell2table.
Note that if E had more than one column, then cell2table(E{1, :}, ...)| would have been equivalent to:
cell2table(E{1, 1}, E{1, 2}, E{1, 3}, ... )
which would have most likely errored unless E{1, 2}, etc. contained valid options for cell2table.
To convert your cell array of cell arrays to a table. First concatenate all the rows so you have a 4x4 cell array, then call cell2table:
T = cell2table(vertcat(E{:}), 'VariableNames',{'IO_Type', 'Name', 'Type', 'Range'});
You may want to review how you construct E so it is a 4x4 cell array (of char arrays) instead of a 4x1 cell array of 1x4 cell arrays (of char arrays)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by