MATLAB: Substitute for join/ outerjoin in cell array

cell arrayjoin;outerjoin

It seems join and outerjoin only work for tables. Is there a substitute for cell arrays?

Best Answer

Well, you can always convert your cell arrays to a table with cell2table, do the join and convert back.
Otherwise, it's not particularly hard to implement using ismember:
%INPUTS:
%leftcell: first cell array
%rightcell: second cell array
%leftkeys: column indices for left key
%rightkeys: column indices for right key
%JOIN:
[common, rows] = ismember(leftcell(:, leftkeys), righcell(:, rightkeys), 'rows');
joinedcell = [leftcell(common, :), rightcell(rows, :)];
%OUTERJOIN:
[commonleft, rows] = ismember(leftcell(:, leftkeys), righcell(:, rightkeys), 'rows');
commonright = ismember(rightcell(:, leftkeys), leftcell(:, rightkeys), 'rows');
joinedcell = [leftcell(commonleft, :), rightcell(rows, :)];
joinedcell = [joinedcell; ... normal join
[cell(sum(~commonright), size(leftcell, 2)), rightcell(~commonright, :)]; ... add non matching rows from rightcell
[leftcell(~commonleft, :), cell(sum(~commonleft), size(righcell, 2))]]; ...add non matching rows from leftcell
I've not merged the left and right keys. It's not particularlty hard to do, and you could also select which columns to include in the join, but you should get the idea from the above.