Effacer les filtres
Effacer les filtres

Merge .mat file to one .mat files

4 vues (au cours des 30 derniers jours)
Yi Xien Yap
Yi Xien Yap le 18 Août 2022
Modifié(e) : Stephen23 le 19 Août 2022
Hello,
I have 4 .mat files. The content in the .mat contain different variable name (From 1 - 100 for example). What I need to do is to combine the four output in the 4 .mat files to one. The output path of the .mat files is stored in a cell structure. What is the best way to do this??
For example:
  3 commentaires
Yi Xien Yap
Yi Xien Yap le 18 Août 2022
Each mat file contain different cell array. For example in this mat file contain data from 10-20, and another one maybe go from 21 - 30. I want to merge them to one mat to show for instance from 1 - 50
Yi Xien Yap
Yi Xien Yap le 19 Août 2022
mergestructs = @(a,b,c,d) cell2struct([struct2cell(a);struct2cell(b);struct2cell(c);struct2cell(d)],[fieldnames(a);fieldnames(b);fieldnames(c);fieldnames(d)]);

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 19 Août 2022
Modifié(e) : Stephen23 le 19 Août 2022
Robust and efficient approach which works for any number of .mat files (unlike the verbose solution you found). Note that any duplicate variable names will be overwritten. Where D is your cell array of the filenames:
F = 'merged.mat';
copyfile(D{1},F)
for k = 2:numel(D)
S = load(D{k});
save(F,'-struct','S','-append')
end
Remember to always LOAD into an output variable:
S = load(..)

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by