MATLAB Answers

Could anyone help me how to five different mat files in workspace into a single mat file.

1 view (last 30 days)
jaah navi
jaah navi on 9 Mar 2020
Edited: Stephen Cobeldick on 9 Mar 2020
I am having five different mat files as user1.mat,user2.mat,user3.mat,user4.mat and user5.mat
I want to combine all five mat file into a single mat file.could anyone please help me on it.

  0 Comments

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 9 Mar 2020
One possibility is
user1 = load user1.mat
user2 = load user2.mat
user3 = load user3.mat
user4 = load user4.mat
user5 = load user5.mat
save user.mat
Each individual variable (e.g. user1) stored in user.mat will be a structure with the data from the corresponding file.
It's a shame you ended up in such a predicament. Take a look at this, which might help in the future.

  1 Comment

jaah navi
jaah navi on 9 Mar 2020
In my case user1.mat contains data in 2 rows 10 colums
user2.mat contains data in 5 rows 10 colums
user3.mat contains data in 10 rows 10 columns
user4.mat contain data in 12 rows 10 columns
user5.mat contains data in 15 rows 10 columns
so i want to combine all mat files into a single file which contains 44 rows 10 colums,which means 2 added with 5 added with 10 added with 12 added with 15.
This was the result which i need.
could you please help me on it.

Sign in to comment.


Stephen Cobeldick
Stephen Cobeldick on 9 Mar 2020
Edited: Stephen Cobeldick on 9 Mar 2020
Assuming that each .mat file contains exactly one variable:
P = 'absolute/relative path to where the files are saved';
V = 1:5;
N = numel(V);
for k = 1:N
F = sprintf('user%u.mat',V(k));
S = load(fullfile(P,F));
C(k) = struct2cell(S);
end
M = vertcat(C{:});
save('userAll.mat','M')

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags