- What is your reason for wanting to store these 13 tables in a single 1x13 table? Are you doing this because you want to have a label for each table?
- Where are you getting stuck? Is the issue that the tables are different heights? Table variables must have the same height, but one way to get around this is to put the tables in cell arrays, e.g.
Table of Tables or cell of Tables
219 次查看(过去 30 天)
显示 更早的评论
Hi,
I am preforming machine learning on 13 channels of data and am currently at the feature extraction stage. I have created 13 seperate tables and now which to bring these together into one variable.
I had been trying to create a 1*13 table in which each table element was another table, however I cannot get this to work. I have been able to create a 1*13 cell with each cell element being a table instead. My question is this, is there any benifit to using a tables within cells over tables within tables and vice versa. I will also include some code below, I would like to know how to also make this work for tables within tables too for furture use!
filesTable = array2table(fileNames); % Generate file names to identify each entry
sTruthBuffer = sTruthBuffer';
truthTable = array2table(sTruthBuffer); % Generate ground (clinical) truth
channels = cell(13,1); % generate empty cell for later
for i = 1:13 % For each of the 13 channels
actTable = rows2vars(array2table(act(:,:,i))); % act(ivity) is a 1*2538*13 double
actTable = removevars(actTable,'OriginalVariableNames'); % remove the orignal variale names as junk
actTable.Properties.VariableNames = {'Activity'} % Rename column as something sensible
mobTable = rows2vars(array2table(mob(:,:,i))) % mob(ility) is a 1*2538*13 double
mobTable = removevars( mobTable,'OriginalVariableNames');
mobTable.Properties.VariableNames = {'Mobility'}
complxTable = rows2vars(array2table(complx(:,:,i))) % complx(ity) is a 1*2538*13 double
complxTable = removevars(complxTable,'OriginalVariableNames');
complxTable.Properties.VariableNames = {'Complexity'}
channel = [filesTable,actTable,mobTable,complxTable,truthTable]; % Concatinate all the tables into one table 'channel'
channels{i} = channel ; % for each channel write the generated table 'channel' into a cell array called 'channels',
% this is okay but I think a nested table would be easier to work with?
end
0 个评论
采纳的回答
Seth Furman
2021-11-12
Hi Christopher,
I had been trying to create a 1*13 table in which each table element was another table, however I cannot get this to work.
t1 = table([1;2;3]);
t2 = table([1;2]);
table({t1}, {t2}, 'VariableNames', ["MyLabel1", "MyLabel2"])
It's also worth mentioning that you can store additional metadata in the UserData property of each table, e.g.
t1.Properties.UserData = "MyLabel1";
t2.Properties.UserData = "MyLabel2";
{t1, t2}
5 个评论
Seth Furman
2021-11-19
Christopher, please let me know which of the following workflows meet your requirements and why. From your description it sounds like indexing syntax is most important to you, in which case (2) and (3) seem like they should be the most convenient.
1) Storing the channels as separate elements of a cell array
% Create sample data
rng default
act = 1000 * rand(1, 2538, 5);
mob = 1000 * rand(1, 2538, 5);
complx = 1000 * rand(1, 2538, 5);
% Preallocate a template channel
channel = table('Size', [2538 3], 'VariableTypes', ["double","double","double"], ...
'VariableNames', ["Activity","Mobility","Complexity"]);
% Preallocate 5 channels by copying 'channel'
channels = repmat({channel}, 1, 5);
% Populate channels
for i = 1:length(channels)
channels{i}.Activity = act(:,:,i)';
channels{i}.Mobility = mob(:,:,i)';
channels{i}.Complexity = complx(:,:,i)';
end
channels
channels{1}.Activity
2) Storing the channels as sub-tables
% Preallocate a template channel
channel = table('Size', [2538 3], 'VariableTypes', ["double","double","double"], 'VariableNames', ["Activity","Mobility","Complexity"]);
% Preallocate 5 channels by copying 'channel'
channels = repmat(table(channel), 1, 5);
channels.Properties.VariableNames = "Channel_" + (1:width(channels));
% Populate channels
for i = 1:width(channels)
channels.(i).Activity = act(:,:,i)';
channels.(i).Mobility = mob(:,:,i)';
channels.(i).Complexity = complx(:,:,i)';
end
head(channels)
channels.Channel_1.Activity
3) Storing the channels as pages of a 3-dimensional array
channels = table(pagetranspose(act), pagetranspose(mob), pagetranspose(complx), ...
'VariableNames', ["Activity","Mobility","Complexity"]);
head(channels)
channels.Activity(:,:,1)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!