How to vertically concatenate many tables produced from a for loop
조회 수: 21 (최근 30일)
이전 댓글 표시
I currently have a for loop set up where it runs through a set of files with slightly different file names and reads each one in as a table as seen below:
for k=1:numel(A) % automatically brings in the correct amount of tables
M = readtable(MyFullFilename,'PreserveVariableNames',true);
M
end
where the 'MyFullFilename' variable is specified earlier and each file that is read from my hardrive has a different value of k at the end of it (the above code works completely fine).
Currently the output I get from this program is my 3 tables outputted individually all labelled M. What I would like to do is vertically concatenate all of them so it is one big table. It is important to note that all variables have the same 9 variables so will fit together fine. Ideally I can keep the variable names at the top of the table as they are useful for manipulating the table later on. Right now I only have 3 tables that I want to concatenate but later on there will be many many more that I want to concatenate onto the bottom, hence, I have a for loop set up to gather all of these. Ideally this would mean that the way of concatenating the tables would be within the for loop.
I've looked at all the different questions posted so far but none work in the way I want them to with my code and the closest I've come to the solution is concatenating the same table three times on top of each other for each of the values k which is not what I want.
Thanks for anyone's help.
댓글 수: 0
채택된 답변
Davide Masiello
2022년 11월 8일
I would try something like this
M = [];
for k=1:numel(A) % automatically brings in the correct amount of tables
M = [M;readtable(MyFullFilename,'PreserveVariableNames',true)];
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!