MATLAB Answers

Unstack - How to break a table into equal chunks of data?

3 views (last 30 days)
Chad Bollmann
Chad Bollmann on 7 Apr 2020
Answered: Kelly Kearney on 7 Apr 2020
I want to break one long table consisting of 160000 data points into 200 (or other arbitrary number) of 800 (again arbitrary) sample data windows.
Request some help understanding the unstack function and whether this permits specifying the equivalent of "grab the next 800 data samples and move them into a new table column"? It doesn't seem as if 'GroupingVariables' supports this.
I'm not wedded to using a table function to do this, but I repeatedly run into this issue of ingesting a long data series and needing to segregate the points into equally-sized windows. I'm open to any type of solution.
I currently perform this via the following for loop in a clunky manner:
% Input table is 2 columns; time stamps in column 1 and measurement in column 2
numberofWindows = 200; % number of data windows I can create from my input table
numberofPoints = 800; % number of samples in each window
% create destination table
windowTable = table;
for i=1:numberofWindows
% set start of window based on window length and overlap
startIndex = (i-1)*numberofPoints*(1-overlap)+1;
% set stop of window using window length
stopIndex = startIndex + numberofPoints - 1;
% grab the times and turn into a label
label = strcat(string(data(startIndex,1)),{'-'},string(data(stopIndex,1)));
% grab the data in the window
values2copy = data(startIndex:stopIndex,2);
% paste window data into next column of table
% Now label each window with the start & end times
Thank you!

Answers (1)

Kelly Kearney
Kelly Kearney on 7 Apr 2020
I usually just use splitapply for this sort of thing. (And the kron trick is a nice one for generating repeating blocks of numbers). In the example below, I chose a value for npt that isn't evenly divisible by the table height, just to show that this works for that instance.
T = table((1:10)', rand(10,1), 'variablenames', {'thing1', 'thing2'});
nt = height(T);
npt = 4; % number of points per sub-group
g = kron((1:ceil(nt/npt))', ones(npt,1));
g = g(1:nt);
Tsub = splitapply(@(x) {T(x,:)}, (1:nt)', g);




Community Treasure Hunt

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

Start Hunting!