How to merge a set of struct files?
조회 수: 4 (최근 30일)
이전 댓글 표시
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201424/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
Stephen23
2019년 1월 24일
Are the field names exactly the same in all of the .mat files?
Do all of the structure have exactly the same size?
채택된 답변
TADA
2019년 1월 24일
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));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!