How to convert tables into numeric arrays?

26 次查看(过去 30 天)
Hi I have the data set C_512 (see attached) and I want to make the tables within the cells of the cell array into numeric arrays so that there are no longer any tables in C_512. I used this code below:
% Load the .mat file
load('/path/to/C_512.mat');
% Initialize a new cell array to store numeric arrays
C_512_numeric = cell(size(C_512));
% Loop through each cell and convert its content to numeric arrays
for i = 1:size(C_512, 1)
for j = 1:size(C_512, 2)
T = C_512{i, j};
if istable(T)
C_512_numeric{i, j} = table2array(T);
else
C_512_numeric{i, j} = T; % If it's already an array or matrix
end
end
end
% Save the numeric data to a new .mat file
save('C_512_numeric.mat', 'C_512_numeric');
Unfortunately, C_512_numeric still contains tables. Why is that?
  3 个评论
dpb
dpb about 19 hours 前
Instead of having to go to such levels of coding as @Voss showed, how was the original .mat file created? Unless it comes from an outside source (and if so, I'd rail at them for such an abomination), you should be able to create at least only a one-level array of tables (or arrays) instead of being doubly-nested.
Show us/explain to us the origination of the original file and let's see about cleaning it up to begin with instead of having to then straighten it out after the fact.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2024-7-26,13:37
C_512 is not a cell array of tables; it is a cell array of cell arrays of tables.
% Load the .mat file
load('./C_512.mat');
C_512 % cell array containing cell arrays
C_512 = 7x4 cell array
{11x1 cell} { 6x1 cell} {14x1 cell} {15x1 cell} { 7x1 cell} {10x1 cell} { 7x1 cell} { 5x1 cell} {10x1 cell} {12x1 cell} {10x1 cell} { 7x1 cell} { 7x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell} {21x1 cell} {10x1 cell} {12x1 cell} { 6x1 cell} { 6x1 cell} { 6x1 cell} { 7x1 cell} { 5x1 cell} { 4x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell}
C_512{1} % cell array containing tables
ans = 11x1 cell array
{512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {512x2 table} {320x2 table}
So when your code checks whether C_512{i, j} is a table, it is not; it is a cell array. You'd need to add another loop to iterate over the cells of C_512{i, j} and check whether each cell contains a table.
% Initialize a new cell array to store numeric arrays
C_512_numeric = cell(size(C_512));
% Loop through each cell and convert its content to numeric arrays
for i = 1:size(C_512, 1)
for j = 1:size(C_512, 2)
C = C_512{i, j};
% pre-allocate the cell array C_512_numeric{i, j}
C_512_numeric{i, j} = cell(size(C));
for k = 1:numel(C)
T = C{k};
if istable(T)
C_512_numeric{i, j}{k,1} = table2array(T);
else
C_512_numeric{i, j}{k} = T; % If it's already an array or matrix
end
end
end
end
C_512_numeric % cell array containing cell arrays
C_512_numeric = 7x4 cell array
{11x1 cell} { 6x1 cell} {14x1 cell} {15x1 cell} { 7x1 cell} {10x1 cell} { 7x1 cell} { 5x1 cell} {10x1 cell} {12x1 cell} {10x1 cell} { 7x1 cell} { 7x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell} {21x1 cell} {10x1 cell} {12x1 cell} { 6x1 cell} { 6x1 cell} { 6x1 cell} { 7x1 cell} { 5x1 cell} { 4x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell}
C_512_numeric{1} % cell array containing numeric matrices
ans = 11x1 cell array
{512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {320x2 double}
And you can reduce the number of loops by one by iterating over the elements of C_512 and using linear indexing, rather than iterating over the rows and columns in two loops and indexing with two subscripts:
% Initialize a new cell array to store numeric arrays
C_512_numeric = cell(size(C_512));
% Loop through each cell and convert its content to numeric arrays
for i = 1:numel(C_512)
C = C_512{i};
% pre-allocate the cell array C_512_numeric{i}
C_512_numeric{i} = cell(size(C));
for k = 1:numel(C)
T = C{k};
if istable(T)
C_512_numeric{i}{k} = table2array(T);
else
C_512_numeric{i}{k} = T; % If it's already an array or matrix
end
end
end
C_512_numeric % cell array containing cell arrays
C_512_numeric = 7x4 cell array
{11x1 cell} { 6x1 cell} {14x1 cell} {15x1 cell} { 7x1 cell} {10x1 cell} { 7x1 cell} { 5x1 cell} {10x1 cell} {12x1 cell} {10x1 cell} { 7x1 cell} { 7x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell} {21x1 cell} {10x1 cell} {12x1 cell} { 6x1 cell} { 6x1 cell} { 6x1 cell} { 7x1 cell} { 5x1 cell} { 4x1 cell} { 4x1 cell} { 5x1 cell} { 4x1 cell}
C_512_numeric{1} % cell array containing numeric matrices
ans = 11x1 cell array
{512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {512x2 double} {320x2 double}
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by