Table comparison [String - cell compare]
20 views (last 30 days)
Show older comments
I have 2 tables that contain the same headers of the first row.
- My goal is to see if the cells contains the same values
- if true - contaminate cells to present the values
From the two tables below.
- I need to have my comparison for example : a cell in X would be 1,5 for Y XW
Part of my code is:
for i=1 : size(Table1)
if strcmpi(Table1.X(i), Table2.X(i) ==1)
X12= cellstr(strcat(....., ',' , ..........)
I am just confused.
I am currently getting - conversion to double from cell is not possible.
Table1 Table 2 expected result
X Y X Y X Y
1 XW 5 XW 1, 5 XW
2 XR 4 XR 2, 4 XR
3 XR 3 XR 3 XR
4 XW 2 XZ
5 XX 1 XZ
1 Comment
the cyclist
on 11 Nov 2021
It would be much easier to help if you uploaded the two tables in a MAT file. You can use the paperclip icon in the INSERT section of the toolbar.
Answers (2)
Seth Furman
on 12 Nov 2021
Edited: Seth Furman
on 15 Nov 2021
Take a look at the table functions innerjoin and outerjoin:
- https://www.mathworks.com/help/matlab/ref/table.join.html
- https://www.mathworks.com/help/matlab/ref/outerjoin.html
t1 = table((1:5)', categorical(["XW";"XR";"XR";"XW";"XX"]), 'VariableNames', ["X","Y"])
t2 = table((5:-1:1)', categorical(["XW";"XR";"XR";"XZ";"XZ"]), 'VariableNames', ["X","Y"])
tJoined = innerjoin(t1, t2, "Keys", "Y")
tJoined = mergevars(tJoined, ["X_t1", "X_t2"], "NewVariableName", "X")
[groupNums, Y] = findgroups(tJoined.Y)
X = splitapply(@(x) {unique(x)'}, tJoined.X, groupNums)
table(X, Y)
0 Comments
Image Analyst
on 12 Nov 2021
Try this:
% Create saple data:
x1 = [1;2;3;4;5]
y1 = {'XW'; 'XR';'XR';'XW';'XX'}
x2 = [5;4;3;2;1]
y2 = {'XW'; 'XR';'XR';'XZ';'XZ'}
table1 = table(x1, y1, 'VariableNames',{'X', 'Y'})
table2 = table(x2, y2, 'VariableNames',{'X', 'Y'})
% Create output table.
table3 = table({0}, {'?'}, 'VariableNames',{'X', 'Y'});
count = 1;
for row = 1 : size(table1, 1)
thisString = table1.Y{row};
fprintf('Searching for %s.\n', thisString)
if isequal(table1.Y{row}, table2.Y{row})
% Columns y match.
table3.X{count} = unique([table1.X(row), table2.X(row)], 'stable');
table3.Y{count} = thisString;
count = count + 1;
end
end
table3
You'll see
table3 =
3×2 table
X Y
_______ ______
{[1 5]} {'XW'}
{[2 4]} {'XR'}
{[ 3]} {'XR'}
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!