Is it possible to stack tables?

18 views (last 30 days)
CrazyEngineer
CrazyEngineer on 31 Aug 2016
Answered: CrazyEngineer on 31 Aug 2016
Hi everyone, I'm facing the following problem: I do several testruns. There are about 10 tests per run, which results in 10 .csv files. I read them with Matlab, do some calculations and store about 6 values per test which I write in a table, thereby creating a 6x10 table. Those values are sometimes strings and sometimes numbers. Then I change the setting and do the next test run, evaluate the files and create the next table. I end up with about 8 tables. Now I would like to somehow stack those tables in order to compare (and plot) the influence of the setting on my test results. I would like to do something like:
all_tables(1, :, :) = table1;
all_tables(2, :, : ) = table2;
etc.
and then read the values back, e.g.
val1 = all_tables(:, 2, 1) ;
Is that possible or is there any other format which is easier to handle than tables?
Thank you in advance :)

Accepted Answer

Stephen23
Stephen23 on 31 Aug 2016
Edited: Stephen23 on 31 Aug 2016
As far as I can tell, tables are limited to two dimensions. Here are some ideas for storing an arbitrary set of data of arbitrary sizes:
  1. It is possible to concatenate tables (of course this requires that the columns/variables be of the same class). Simply add a new index variable, which tells you which test the data is from.
  2. You could put the tables into a cell array. This might be the easiest solution.
  3. You could avoid the tables entirely, and just use a 3D cell array. This will be difficult if each test produces different length outputs.

More Answers (2)

Guillaume
Guillaume on 31 Aug 2016
As Stephen says, using cell arrays may be easier.
Alternatively, you could concatenate the tables into one tall table with two extra columns (original table, and row) that you'd use for filtering:
addcols = @(t, id) [t, table(repmat(id, height(t), 1), (1:height(t))', 'VariableNames', {'TableID', 'RowID'})];
all_tables = [addcols(table1, 1);
addcols(table2, 2);
addcols(table3, 2)];
To filter:
%One row, column from all original tables
%instead of all_tables(:, 2, 1):
val1 = all_tables(all_tables.RowID == 2, 1)
%One row, all columns from one table
%instead of all_tables(1, 2, :):
val2 = all_tables(alltables.TableID == 1 & alltables.RowID == 2, :)

CrazyEngineer
CrazyEngineer on 31 Aug 2016
Thank you Stephen Cobeldick and Guillaume :) I switched to cell arrays just as you suggested and after a bit of playing around it works just fine!

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!