Combining matrices from workspace or from directory into a single matrix.
10 views (last 30 days)
Show older comments
Sachin Patalasingh
on 17 Jul 2020
Commented: Sachin Patalasingh
on 18 Jul 2020
Can someone explain how to combine .mat files present in workspace or in directory into one .mat file? The problem is that the files are not properly named and the files have different rows but same number of coloumns.
3 Comments
David Hill
on 17 Jul 2020
If they are in the workspace, then they are already loaded. If they all have the same numbe of columns, then:
newMatrix=[a;b;c;d;e;f;g;h;i];%where a,b,c,d ... are your other maxtrics.
Accepted Answer
Stephen23
on 17 Jul 2020
Edited: Stephen23
on 18 Jul 2020
Assuming that each .mat file contains exactly one array with compatible sizes:
D = 'path to the directory where the files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
C(k) = struct2cell(load(F));
end
M = vertcat(C{:})
save('new.mat','M')
3 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!