Reputation: 199
I have a couple of files .mat-files named "K1.mat", "K2.mat" etc. Each file contains a matrix called K of the same size.
What I would like to do is load K from each file as "K1", "K2" etc. and assemble them into a bigger matrix K = [K1; K2; ...; Kn]. But I cannot seem to find a way to load the K-matrix from the K(i).mat-files the way I'd like to. Using the load() command matlab loads the matrix as "K", but I'd like to load it and directly assign it to the variable "K1", "K2" etc. I suppose I could do this by loading K and do something like K(i) = K, but this seems unnecessarily complex. Is there an easier way to do this?
Upvotes: 2
Views: 977
Reputation: 114936
When loading to a variable (e.g., K=load('K1.mat')
) the variable K
in your workspace is a struct
with a field for each variable stored in the mat file. In your case it will by K.K
.
What you can do is
K = []; % consider pre-allocating
for ki=1:n % n mat files
tmp = load( sprintf('K%d.mat', ki) );
K = cat( 2, K, tmp.K );
end
Upvotes: 4