Creating a multidimensional table

I'd like to create a multidimensional table. For example a 3D table with 10 stacked 3x3 matrices.
I tried with this code, but it doesn't work:
R = zeros(3,3,10);
T = table(R);
Thank you in advance.

8 个评论

If you mean a table with 3 or more index positions, then that is not supported. Nested tables are supported though.
Hello @WalterRoberson, can you tell me how create nested tables please? I uploaded an example of what I want (a table with inside 2 tables).
? That example just has a numeric array and a cell array
data = data_statics(:); label = label_statics(:);
table(data, 'rownames', label)
Hi @WalterRoberson, I want to create a file like the one I uploaded (file with inside 2 dataset). How can I do it? Ps: sorry, maybe I called it in a wrong way.
T1 = table(data_statics);
T2 = table(label_statics);
T = table(T1,T2);
T =
1×2 table
T1 T2
data_statics label_statics
______________ _____________
[1x138 double] [1x138 cell]
But you might prefer
data_statics = data_statics(:);
label_statics = label_statics(:);
data_statics_table = table(data_statics);
label_statics_table = table(label_statics);
T = table(data_statics_table, label_statics_table);
It hardly seems worth putting each variable in its own table, though.
Good morning @WalterRoberson. Maybe I explained the problem bad due to my english. The goal is to create a file like data_statics.mat starting from data I created in the workspace for example (e.g. A=[1 1]; B=[2 2];).
Expected result: file.mat. When I load file.mat, I'd like to double click on it in the workspace and to see automatically 2 new "variables" (A and B) in the workspace.
This happens when I double click on data_statics; notice that after double click on data_statics, there is no track of data_statics in the workspace, there are only the 2 "variables": data_statics (same name, but different from data_statics I mentioned before) and label_statics.
Thank you for your patience.
@WalterRobinson If I wanted to access a certain range in the double under one of the variables, how would I do so using your example? This page does not seems to help: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
In case anyone wants to know, I figured it out:
T.data_statistics(row #, n, m) , where n and m are the row and column dimensions, respectively, of the double under the variable name 'data_statistics'

请先登录,再进行评论。

回答(3 个)

With R2018b
>> R = zeros(3,3,10);
>> T = table(R);
>> T
T =
3×1 table
R
_______________
[1x3x10 double]
[1x3x10 double]
[1x3x10 double]
>>
What do you mean by "it doesn't work"

8 个评论

Hello @perisakson, when I double click on T in the workspace, I can't see a table with more "sheets". I want to see in the first sheet a 3x3 matrix, on the second the same on so on.
"multidimensional table" in Matlab would be an array of tables, but that is not supported.
See the question How to create table array in MATLAB? in Stack Overflow.
Matlab supports tables with tables as values of variables
>> T = table(T2,T3)
T =
3×2 table
T2 T3
____________ ____________
[1x10 table] [1x10 table]
[1x10 table] [1x10 table]
[1x10 table] [1x10 table]
and cell arrays of tables.
Hi @perisakson, I tried the code you suggested me. If I click on table T=table(T1,T2) I don't get two different variables in workspace as in the file I uploaded (load data_statics please).
I actually did download it, but failed to figure out how it supports your question. See Walters comment
I've deleted this comment because it contained too many mistakes on my part.
New try with R2018b
%%
T1 = table( ones(3,1) );
T2 = table( 2*ones(3,1) );
T = table( T1, T2 )
returns
T =
3×2 table
T1 T2
Var1 Var1
____ ____
1 2
1 2
1 2
T2 = mergevars(___,'MergeAsTable',true) merges the specified variables into a table, instead of an array. The new table is itself a variable of the output table T2.
So, Yes, you can have tables as variables inside a table.
mergevars was "Introduced in R2018a."
I learn a lot tonight :)
R2015b experiment:
>> load data_statics
>> T1 = table(data_statics);
T2 = table(label_statics);
T = table(T1,T2);
>> T
T =
T1 T2
___________ ___________
[1x1 table] [1x1 table]

请先登录,再进行评论。

Mike D.
Mike D. 2019-7-9
T = table(T1,T2,T3) only works if all three tables have exactly the same number of columns AND exactly the same number of rows, and all columns are the same data type. If I had a 1000 tables, it would be nice if Matlab could index them:
T(1) = T1;
T(2) = T2;
for i = 1:1000
T(i) = readtable(sprintf('file%d.dat', i), opts);
end

1 个评论

Use cell array.
T = cell(1000,1);
T{1} = T1;
T{2} = T2;
for i = 1:1000
T{i} = readtable(sprintf('file%d.dat', i), opts);
end
Remember that for tables, the syntax T(i) is a shortcut for T{:,i} so for arrays of tables T(i) would be an ambiguous syntax unless arrays of tables were a different datatype.

请先登录,再进行评论。

Works great, thanks. When I plot all thousand tables, each having thousands of rows and multiple columns, I couldn't do it with a one-liner such as:
plot(T{:}{:,1},T{:}{:,2})
Instead, I had to use a for-loop:
for i = 1 : numel(T)
plot(T{i}{:,1},T{i}{:,2})
plot(T{i}.Longitude, T{i}.Latitude)
end
But it works, thanks.

类别

帮助中心File Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by