How to convert tables into numeric arrays?

3 views (last 30 days)
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 Comments
dpb
dpb on 26 Jul 2024
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.

Sign in to comment.

Accepted Answer

Voss
Voss on 26 Jul 2024
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 Comments
lil brain
lil brain on 26 Jul 2024
Awesome and very helpful thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!