How to concatenate tables with the same number of variables but different number of rows?

1 view (last 30 days)
I have a series of tables (AA.mat, BB,mat, CC.mat, etc...17 in total), each of them with diiferent number of rows but same number of variables (24x4, 21x4, 3x4, 29,x4, etc.). How can I merge all these tables into a single one, so it vertically concatenates rows? Thank you!

Answers (2)

Guillaume
Guillaume on 6 Dec 2018
AA.mat, etc. are mat files not tables. Each of these file could contain any number of variables. Possibly, each of these only contain one variable, a table but that's not what you've actually described. If it is the case,
filelist = {'AA.mat', 'BB.mat', '...'}; %list of files containing the tables, possibly obtained by dir
folder = '???'; %possibly equal to pwd
fulltable = cell(size(filelist)); %cell array to receive the table contained in each file
for fileidx = 1:numel(filelist)
filecontent = load(fullfile(folder, filelist{fileidx})); %load file
varlist = fieldnames(filecontent); %get list of variables in the file
assert(numel(varlist) == 1, 'File %s contains more than one variable', filelist{fileidx}); %check that there is only one variable in the file
fulltable{fileidx} = filecontent.(varlist{1}); %extract the one variable in the cell array
assert(isa(fulltable{fileidx}, 'table'), '%s in %s is not a table', varlist{1}, filelist{fileidx}); %check that the one variable is indeed a table
end
fulltable = vertcat(fulltable{:}); %concatenate all the tables
  6 Comments
vicsm
vicsm on 6 Dec 2018
I will like to concatenate the four of them into a single table.
And yes, those are the "only" 17 .mat files I have in that folder.
Guillaume
Guillaume on 6 Dec 2018
You need to explain better what you have. mat files, variables, and tables are completely different things. My understanding:
  • you have 17 mat files (with absolutely rubbish names!)
  • each of these 17 mat files contain 4 variables always named participant, mean_RT, RT_date, RT_time, possibly these variables are tables
Now, do you want to produce 17 tables, one for each file, where each table is the concatenation of the 4 variables (that's what you appear to ask)?
Or do you want to produce 4 tables, one for each variable, where each table is the concatenation of the respective variable in each file (that would make more sense)?
Or do you want to produce just 1 table, which is the concatenation of all the variables in all of the files?

Sign in to comment.


Stephen23
Stephen23 on 6 Dec 2018
Edited: Stephen23 on 6 Dec 2018
Assuming that the data have the same class, that each .mat file contains the same four variables, and that within any one mat file they all have the same number of rows... try something like this:
D = 'path to folder where the .mat files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
C = cell(N,4);
for k = 1:N
T = load(fullfile(D,S(k).name));
T = orderfields(T);
C(k,:) = struct2cell(T).';
end
M = cell2mat(C)

Community Treasure Hunt

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

Start Hunting!