How to vertically concatenate many tables produced from a for loop

24 views (last 30 days)
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.

Accepted Answer

Davide Masiello
Davide Masiello on 8 Nov 2022
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!