cell2table only using first cell array row

4 次查看(过去 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