Assembly of matrices using for loop

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
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
Silver el 1 de Sept. de 2018
Thank you for answering.It does for this exemple, but for larger amounts of data, I need to use the For loop, and that's the problem.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 31 de Ag. de 2018

2 votos

Read each individual file into a cell array element, then vertically concatenate the cells all at once.

2 comentarios

Silver
Silver el 1 de Sept. de 2018
Thank you James Tursa for answering. However I would like to know when you said "Read each individual file into a cell array element" Does this script work ?
for i=1:data_ver %data_ver is the nmber of line (size) of the matrix
for j=1:data_hor %data_hor is the nmber of columns(size) of the matrix
temp = cell2mat(cellstr(Data(i,j))) ; %fill matrix
A(i,j)=str2double(temp) ;
end
end
And what about "vertically concatenate the cells all at once" I dont see what that means. Thank you again.
Stephen23
Stephen23 el 1 de Sept. de 2018
Editada: Stephen23 el 1 de Sept. de 2018
@Silver: you do not need to use any loops, as MATLAB's file importing functions are quite capable of importing the entire file by themselves. Also importing the data as character and then using str2double is most likely a misdirection: MATLAB's file importing function already import numeric data as numeric. So none of your code is very useful, or a good way to approach this task. A much better approach is to read the documentation:
and pick a suitable method for reading your files.
"And what about "vertically concatenate the cells all at once" I dont see what that means"
Once you have all of the imported numeric data into one cell array, after the loop you can vertically concatenate the imported data into one matrix, exactly as you requested, e.g.:
mat = vertcat(C{:})

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 1 de Sept. de 2018
Editada: Stephen23 el 1 de Sept. de 2018
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
Silver el 1 de Sept. de 2018
Thank you so much @Stephen Cobeldick , I will try the code and follow the instructions in the documentation.
Silver
Silver el 3 de Sept. de 2018
Editada: Silver el 3 de Sept. de 2018
Thank you so much @Stephen Cobeldick, it really works!! Instead, am looking forward to export the structure into a excel file. Do you have any idea, cause am not realy skilful with structure. I tried this script, it didnt work:
writetable(struct2table(out), 'outputfile.xlsx') % out is the structure you put it in your comment.
Thanks in advance.
Stephen23
Stephen23 el 4 de Sept. de 2018
Editada: Stephen23 el 4 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
Silver el 4 de Sept. de 2018
It realy works!! Thank you so much :D , I apreciate your help that saves me x) !! Thank you again
Silver
Silver el 4 de Sept. de 2018
Am sorry for disturbing,but I have one last question @stephen Cobeldick >< : Actually in the program I added
G{k} = T.textdata;
And I want to extract data from T.textdata, but I want to take the first column starting from the second line till the end, like i mentionned in the photo below :
So when I write this code :
time = vertcat(G{:}{(2,:),1})
It does not work , and I get the error message "Expected one output from a curly brace or dot indexing expression, but there were 2 results." Should I for exemple put define the size of the T.textdata first ? Thanks again.
Stephen23
Stephen23 el 5 de Sept. de 2018
Editada: Stephen23 el 5 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
Silver el 5 de Sept. de 2018
You rock !! Thank you so much it works, and thanks for explanation , appreciate that!
Silver
Silver el 6 de Sept. de 2018
It really helps me ! thank you again , For sure I've accepted the answer and flagged it ^^

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 31 de Ag. de 2018

Comentada:

el 6 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by