Effacer les filtres
Effacer les filtres

Saving a table-type variable to MAT file column by column in For-loop

2 vues (au cours des 30 derniers jours)
Smithy
Smithy le 11 Août 2022
Commenté : Smithy le 12 Août 2022
Hello everybody,
I have big size of a table-type variable.
I am trying to save a table-type variable to .mat file column by column as table type in in For-loop.
I tried below code but it is not table and VariableNames disappear.
Is there a way to save .mat file as table type?
var = [num2cell(rand(4,3)), {'Steve';'John';'Paul';'Evan'}];
A = splitvars(table(var));
A.Properties.VariableNames = ["Aa","Bb","Cc","Dd"];
for i = 1:width(A)
colData = A.(i); % this case colData is not table type and column name disappear.
save([pwd,'\AA_' num2str(i) '.mat'],'colData')
end

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Août 2022
outdir = pwd();
var = [num2cell(rand(4,3)), {'Steve';'John';'Paul';'Evan'}];
A = splitvars(table(var));
A.Properties.VariableNames = ["Aa","Bb","Cc","Dd"];
for i = 1:width(A)
clear S
thisvar = A.Properties.VariableNames{i};
S.(thisvar) = A(:,i);
save( fullfile(outdir, "AA_" + i + ".mat"), '-struct', 'S')
end
!ls
AA_1.mat AA_2.mat AA_3.mat AA_4.mat
whos -file AA_1.mat
Name Size Bytes Class Attributes Aa - 1427 table
  1 commentaire
Smithy
Smithy le 12 Août 2022
Thank you very much for your huge helps. It works very well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Preprocessing Data dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by