How to convert tables into numeric arrays?
Afficher commentaires plus anciens
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 commentaires
whos -file C_512.mat
load C_512
C_512
C_512{1,1}
C_512{1,1}{1}
C_512{1,1}{2}
Wowsers! What a mess! How did you manage to dig yourself into such a hole? My suggestion would be to go back to the code that generated this deeply nested thing and build a better data structure to start with and avoid the question as posed altogether.
lil brain
le 26 Juil 2024
dpb
le 26 Juil 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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!