How to loop over a series of files

I have 10 datasets named data1, data2,data3,...,data10 with dimensions [150,120, 25, 5]. I want to create a big matrix and put all this data in.
The new matrix should have dimensions [150,120,25, 5, 10]. I tried to make a for loop but I got it wrong. Below is my code
Big_matrix = zeros(150,120,25,10);
for i=1:10
Big_matrix(:,:,:,:,i)=data(i);
end
Can anyone please help with that?

5 Comments

"I have 10 datasets named data1, data2,data3,...,data10"
Numbered variables are a sign that you are doing something wrong.
Trying to access variable names dynamically is how some beginners force themselves into writing slow, complex, obfuscated, buggy code. Read this to know some of the reasons why:
You should use indexing. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do). You can trivially use idnexing to put your data into one cell array when you load/import the data, then you can simply do this:
A = cat(5,C{:})
Better data design lets you write simpler, neater, more efficient code.
Gina Carts
Gina Carts on 14 Mar 2019
Edited: Gina Carts on 14 Mar 2019
I should have wrote it like that D1, D2,..D10? That's what you meant?
Then I tried the following:
A = cat(4,D{:});
But I got an error "Undefined function or variable 'D'."
No, you shouldn't have separated them into different variables. If you had made them elements of a cell array you could have done it like that. One of the advantages of a cell array is that each element can contain its own data type and size array, so you can process each separately until you are ready to merge them.
D=cell(1);
D{1}=rand(2,3,4);
D{2}=rand(2,3,4);
D{3}=rand(2,3,4);
D{4}=rand(2,3,4);
D{5}=rand(2,3,4);
A = cat(4,D{:});
size(A)%returns 2,3,4,5
For now you can use my answer below to work around your problem. (I wouldn't call it a solution since it is much better to prevent the problem from occuring in the first place)
I've been trying to do what you suggested but I really struggle with dimensions. My case is 4D and I am trying to make a 5D matrix.
D=cell(1);
D{1}=rand(2,3,4,5);
D{2}=rand(2,3,4,5);
D{3}=rand(2,3,4,5);
D{4}=rand(2,3,4,5);
D{5}=rand(2,3,4,5);
A = cat(4,D{:});
size(A)
ans =
2 3 4 25
It's adding up the 4th dimension. I was expecting to have a 5th dimension as in your example that you ended up with a 4D matrix.
Would it be correct to use the reshape function to make it 5D? i.e. B = reshape(A, 2,3,4,5,5)?
Have you read the documentation for the cat function? The first input is the dimension in which your arrays (the later inputs) should be concatenated. So to concatenate in the 5th dimension, use cat(5,D{:}) instead.

Sign in to comment.

Answers (1)

Your mistake was numbering your variables. However, you can use eval to solve your current problem.
You should avoid eval. Make sure to never need it by having a better data structure.
Big_matrix=eval(['cat(4' sprintf(',data%d',1:5) ')']);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Mar 2019

Edited:

on 15 Mar 2019

Community Treasure Hunt

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

Start Hunting!