How can I store a table inside a matrix?

2 次查看(过去 30 天)
I have a large table which I want to break down into smaller tables based on the time period each entry has. I'm using this code to do that:
i = 1;
while i < height(data)
%crete an empty table with correct headers
subset = data;
subset(:,:) = [];
%populate subset based on time
t1 = data.time(i);
tempcount = 1;
while hours(data.time(i)) < hours(t1)+1 && i < height(data)
subset(tempcount,:) = data(i,:);
i = i + 1;
tempcount = tempcount + 1;
end
end
However this will of course only output the final hour block. What I want to do is store each subset table after its created in a 1xN matrix so that I can reference these individually later on, but I'm unsure how to go about this.

采纳的回答

Rani V.S
Rani V.S 2016-10-26
Matrix cannot hold tables. Better solution is to create a cell array and store tables in each cells

更多回答(1 个)

Peter Perkins
Peter Perkins 2016-10-26
As Nidhikutty says, if you want to split data into hourly (?) tables and keep all of those in one container, you'll need to store them in a cell array.
One other point: you can almost certainly replace your inner loop with something faster along the lines of
i2 = find(data.time < data.time(i1)+hours(1), 1, 'last')
subset = data(i1:i2,:);
There are also ways to avoid even the outer loop. One of them might be
>> t = table(randn(5,1),randn(5,1),hours(2*rand(5,1)))
t =
Var1 Var2 Var3
________ ________ __________
2.908 -0.27247 0.52761 hr
0.82522 1.0984 0.29108 hr
1.379 -0.27787 0.27214 hr
-1.0582 0.70154 1.7386 hr
-0.46862 -2.0518 1.1594 hr
>> t.Var4 = floor(t.Var3,'hour')
t =
Var1 Var2 Var3 Var4
________ ________ __________ ____
2.908 -0.27247 0.52761 hr 0 hr
0.82522 1.0984 0.29108 hr 0 hr
1.379 -0.27787 0.27214 hr 0 hr
-1.0582 0.70154 1.7386 hr 1 hr
-0.46862 -2.0518 1.1594 hr 1 hr
>> rowfun(@(x,y,z) table(x,y,z), t, 'GroupingVariables','Var4', 'OutputFormat','cell')
ans =
[3×3 table]
[2×3 table]
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by