How to convert tables into numeric arrays?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lil brain
el 26 de Jul. de 2024
Comentada: dpb
el 26 de Jul. de 2024
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 comentarios
dpb
el 26 de Jul. de 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.
Respuesta aceptada
Voss
el 26 de Jul. de 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{1} % cell array containing tables
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{1} % cell array containing numeric matrices
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{1} % cell array containing numeric matrices
3 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!