Creating a 3D table (or equivalent)

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?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 16 日

3 投票

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 件のコメント

Peter
Peter 2020 年 4 月 16 日
Thanks, this works. I ended up adding another column to the cell array for name of table:
stackedData = cell(2,dataSetCount);
for i=1:dataSetCount
stackedData{1,i} = dataSetName; %name concatenation not show, this is a unique name
table = ImportDataTable() %input arguments not show, this gets a new data table
stackedData{2,i} = table{:,:};
end
stackedData{1,:} %the names of the data sets (formated time, in my case)
stackedData{2,:} %the data
Ameer Hamza
Ameer Hamza 2020 年 4 月 16 日
Although it is a matter of convenience, if you also want to save names and tables, then I suggest using structs. Something like this
stackedData = repmat(struct('Name', '', 'Table', []), 1, dataSetCount);
for i=1:dataSetCount
stackedData(i).Name = dataSetName; %name concatenation not show, this is a unique name
table = ImportDataTable() %input arguments not show, this gets a new data table
stackedData(i).Table = table{:,:};
end
Peter
Peter 2020 年 4 月 17 日
It looks like when I assign the table to the fielf "Table" in the struct, that data is no longer represented as a table, but as a struct. Is that right?
So I can't call
stackedData(1).Table.NameOfCol1
Because stackedData(1).Table is no longer actually a table. That gives an error "Dot indexing is not supported for variables of this type". It has no longer has a field 'NameOfCol1'. Whereas if I keep a reference to table, I CAN still access each column (and extract it's data) by name in it:
table.NameOfCol1
Ameer Hamza
Ameer Hamza 2020 年 4 月 17 日
I guess this is happening because you are first extracting the content of the table and then assigning it to an element of the cell array. Change this line to
stackedData(i).Table = table{:,:};
change it to
stackedData(i).Table = table;
Also, I suggest changing the name of variables in these two lines from the table to something else. table is also the name of MATLAB's built-in function, it can cause an error later.
T = ImportDataTable() %input arguments not show, this gets a new data table
stackedData(i).Table = T;
Peter
Peter 2020 年 4 月 17 日
That's it, thanks again!
Ameer Hamza
Ameer Hamza 2020 年 4 月 17 日
Glad to be of help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2020 年 4 月 16 日

コメント済み:

2020 年 4 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by