Creating a 3D table (or equivalent)

162 visualizzazioni (ultimi 30 giorni)
Peter
Peter il 16 Apr 2020
Commentato: Ameer Hamza il 17 Apr 2020
Is it possible to create a 3D table to represent a stack of tables? I have N number of tables, each of which has the same number of columns and rows, the same column and row names, and the same type of data in each column. I'm looking for a way to stack them, so that each data set can be referenced by either it's string name, or the index in the stack.
You can create a table of tables like this:
initTable1 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'})
initTable2 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'})
initTable3 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'})
stackTable = table(initTable1,initTable2,initTable3)
But I want to be able to define the number of tables that are included in 'stackTable' on the fly. How would I initialize such a table? The number of stacked tables will not be static (and will probably be on the order of 6-30 stacked tables). If it matters, the size of each individual table is relativly small.
I'm trying to do this because I'd like to be able to reference each table by either it's string name or by it's index, similar to how you can access each field in a traditional 2D table.
If this is not possible, or not advised, is there a better alternative?

Risposta accettata

Ameer Hamza
Ameer Hamza il 16 Apr 2020
Instead of making tables of tables, I recommend making a cell array of tables because stacking makes more sense in that case. For example
initTable1 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'});
initTable2 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'});
initTable3 = table('Size',[6,4],'VariableTypes',{'string','double','double','double'});
stackTable = {initTable1, initTable2, initTable3};
Now you can get the data using the index of the table in stack and the variable name.
stackTable{index_in_stack}.variableName
  6 Commenti
Peter
Peter il 17 Apr 2020
That's it, thanks again!
Ameer Hamza
Ameer Hamza il 17 Apr 2020
Glad to be of help.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by