Assembly of matrices using for loop
Mostrar comentarios más antiguos
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 comentarios
madhan ravi
el 31 de Ag. de 2018
Editada: madhan ravi
el 31 de Ag. de 2018
C=[A;B] this will give the result for the example?
Silver
el 1 de Sept. de 2018
Respuesta aceptada
Más respuestas (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 comentarios
Silver
el 1 de Sept. de 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
el 4 de Sept. de 2018
Silver
el 4 de Sept. de 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
el 5 de Sept. de 2018
Silver
el 6 de Sept. de 2018
Categorías
Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
