Assembly of matrices using for loop
Show older comments
I have 20 excel files containing matrix, each matrix has 49 columns (all the matrix has the same number of columns) and a variable number of lines (the number of lines is not always the same for all the matrix I have). I want to assemble all of them in one excel file , what makes a new matrix with 49 columns.This should not be addition but assembly. For exemple if I have these two matrix : A = [1 2;3 4] B = [5 6;7 8] The result I wish to have is a new matrix C while : C =
1 2
3 4
5 6
7 8
In other words it is sort of like joining the matrices but by adding each matrix to the previous one, successively from the line where the previous matrix has stopped while keeping the same number of columns (49).
Any idea how to do this? Thanks in advance
2 Comments
madhan ravi
on 31 Aug 2018
Edited: madhan ravi
on 31 Aug 2018
C=[A;B] this will give the result for the example?
Silver
on 1 Sep 2018
Accepted Answer
More Answers (1)
Following the examples in the MATLAB documentation:
N = 20;
C = cell(1,N);
for k = 1:N
F = sprintf('file%d.txt', k);
C{k} = importdata(F);
end
out = vertcat(C{:})
Or use dir, as shown in the link I gave above.
8 Comments
Silver
on 1 Sep 2018
Inside the loop get the required data, e.g.:
N = 20;
C = cell(1,N);
for k = 1:N
F = sprintf('file%d.txt', k);
T = importdata(F);
C{k} = T.data;
end
out = vertcat(C{:})
Remember to accept the answer that helped you most to resolve your original question!
Silver
on 4 Sep 2018
Silver
on 4 Sep 2018
Extract the required data before putting it in C, something like this:
G{k} = T.textdata(2:end,1);
...
time = vertcat(G{:});
Here is an explanation of why your syntax does not work:
Silver
on 5 Sep 2018
Silver
on 6 Sep 2018
Categories
Find more on Data Import from 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!