Effacer les filtres
Effacer les filtres

How to merge a set of struct files?

4 vues (au cours des 30 derniers jours)
ELCIO S
ELCIO S le 24 Jan 2019
Commenté : ELCIO S le 25 Jan 2019
Hi everyone. I have a lot of structs saved in .mat files. I need merge all of then in just one. How can I do it?
Let suppose, I have 4 .mat file, like in attached ones. Each of then are a 1x3 struct with 4 fields, like in figure below.
fig1.png
I need merge then to obtain a new one with 1 x 12 struct with 4 fields.
Can anyone help me?
Thank you.
  2 commentaires
Stephen23
Stephen23 le 24 Jan 2019
Are the field names exactly the same in all of the .mat files?
Do all of the structure have exactly the same size?
ELCIO S
ELCIO S le 24 Jan 2019
Modifié(e) : ELCIO S le 24 Jan 2019
@Stephen Cobeldick Yes, same field names and size.

Connectez-vous pour commenter.

Réponse acceptée

TADA
TADA le 24 Jan 2019
files = dir('data_*.mat'); % based on the names of the files you supplied
c = cell(1,numel(files));
for i = 1:numel(files)
filename = fullfile(files(i).folder, files(i).name);
c{i} = load(filename);
end
% get an array of the data structs
data = cell2mat(c);
% or you can unravel the data struct into an array of the data itself:
data1 = cell2mat(cellfun(@(a) a.data, c, 'UniformOutput', false));
  1 commentaire
ELCIO S
ELCIO S le 25 Jan 2019
Thank you. It works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by