Azzera filtri
Azzera filtri

Select specific rows in Matlab Table.

40 visualizzazioni (ultimi 30 giorni)
John Randelin
John Randelin il 15 Mag 2020
Modificato: Adam Danz il 26 Giu 2020
I'm having trouble with my matlab table, the situation is as follows:
My table have nine columns.
I need to create a total of three new tables (M2,M3 and M4), and then group the data into one of the new tables depending on the ”RecordTime”-column.
If there are two identical ”Recordtimes” after one another, I need to create a new table (M2) and add the rows, if there are two identical again, I need to copy and add them to the same table (M2).
For example: Part of my table looks like this:
As you can see, I have two identical times, so the first two rows should be added to my M2 Table. The following two times are also identical, so they should also be copied to the same M2 table.
Some rows will have three identical times, they should then be copied to a new Table called M3, there can even be four identical times, which I need to put in my M4 table.
The dates are already sorted in ascending order, and there will only be two, three or four identical times, never five or six. Every time has at least one pair.
Any help or input here would be appreciated!
Thanks in advance!

Risposta accettata

Eric Sofen
Eric Sofen il 25 Giu 2020
I would suggest counting the unique times, then you can split up the table based on the counts:
d = datetime(2018,1,1,[1 2 4 4 2 1 2 3 4 4]',0,0);
t = table(d,rand(size(d)),'VariableNames',["time","data"]);
[C, ia, ic] = unique(t.time,'stable');
d_counts = accumarray(ic,1)
% expand the counts back out to the height of the table
d_counts = d_counts(ic)
t.counts = d_counts;
% split up the table
t_1 = t(t.counts == 1,:)
That said, do you really need to split up the table? Labeling variables numerically like that is usually a sign that you're heading down a path to breakable code. Now that you have the counts in your table, you can dynamically subscript into t and temporarily get the subset that you need. Or, use groupsummary or grouped varfun to apply functions to the whole table, grouped by count.

Più risposte (1)

Adam Danz
Adam Danz il 25 Giu 2020
This demo detects groups of duplicate time stamps using the diff() function and then splits the timetable rows into 3 tables contain 2-duplicate times, 3-duplicate times, and 4-duplicate times.
% Create timetable with 2, 3, & 4 timestamp duplicates
Time = datetime(2000,1,1) + minutes(0:4:28)';
Time = Time([1 1 2 2 2 3 3 4 4 4 4 5 5 5 6 6 7 7 8 8 8], :);
data = rand(size(Time));
TT = timetable(Time,data);
% Make sure the timetable is sorted by the "Time" column
TT = sortrows(TT, 'Time');
% Detect rows that have an increase in time. The seconds()
% argument is used in case there is roundoff error.
isNewTime = ~[false; diff(TT.Time) < seconds(0.001)];
% count number of duplicates in each group
nDuplicates = flipud(diff([0;find(flipud(isNewTime))]));
% Identify the start and end row numbers for each group of duplicates
groupStarts = find(isNewTime);
groupEnds = [groupStarts(2:end);height(TT)+1] -1;
rowSubs = @(n)[groupStarts(nDuplicates==n), groupEnds(nDuplicates==n)];
rowSelection = @(m)cell2mat(arrayfun(@(i){m(i,1):m(i,2)},1:size(m,1)));
% Split into 3 tables
TT_2pairs = TT(rowSelection(rowSubs(2)),:);
TT_3pairs = TT(rowSelection(rowSubs(3)),:);
TT_4pairs = TT(rowSelection(rowSubs(4)),:);
% Check that the combined & sorted sub-tables is the same as the master table
% This should return TRUE (1) to indicate success
isequal(sortrows([TT_2pairs; TT_3pairs; TT_4pairs],'Time'), TT)
I agree with Eric Sofen that keeping the master table together is usually the best approach, and to use indexing instead.


Scopri di più su Tables in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by