VertCat unique rows of multiple tables.
17 views (last 30 days)
Show older comments
Hi All,
I've got myself in a bit of a muddle and am looking for some help.
I have a number of tables containing the same variables but different rows of data. Some of the data is duplicated between tables.
The first column contains filenames stored in a categorical array.
I would like to vertically concatenate the tables after filtering out duplicate rows of data defined by duplicate filenames.
Is there a way to check for duplicates within two separate category arrays, then use that as a logical mask to select the rows to append.
%% Example Table structure.
tAll = table( 'Size' ,[0,5],...
'VariableNames' ,{'FileName' ,'SampleNumber' ,'DataType' ,'Value' ,'cellData'},...
'VariableTypes' ,{'categorical' ,'uint8' ,'categorical' ,'double' ,'cell'});
So what I want to do is something like...
tAll = [ t1 ; t2(t2.FileName ~= categories(t1.FileName))] % this doesn't work because t1.FileName and t2.FileName are arrays
I understand Joining tables wouldn't be appropriate, as there is no relationship as such, they are all the same variables.
I have also tried...
tAll = union( t1 ; t2 ); % this give an error because one variable contains random numbers of cells.
% Error using tabular/union (line 42)
% Unable to group rows using unique values of the table variable 'cellData'. UNIQUE returned an error.
% Caused by:
% Error using matlab.internal.math.uniqueCellstrHelper
% Cell array input must be a cell array of character vectors.
TIA, MC.
0 Comments
Accepted Answer
Bruno Luong
on 25 Apr 2022
% dummy test data
Filename1=["a"; "b"; "c"];
Data1=["a1"; "b1"; "c1"];
T1=table(Filename1,Data1,'VariableNames',{'Filename','Data'})
Filename2=["a"; "d"];
Data2=["a2"; "d2"];
T2=table(Filename2,Data2,'VariableNames',{'Filename','Data'})
[~,i]=setdiff(T2.Filename, T1.Filename);
[T1; T2(i,:)]
2 Comments
Bruno Luong
on 25 Apr 2022
You migh try
tAll = [T1; T2(~ismember(T2.Filename, T1.Filename),:)];
More Answers (1)
See Also
Categories
Find more on Categorical Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!