www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/MergeKeyVariablePairtoSingleVariableExample.m

    %% Merge Key Variable Pair to Single Variable  

%% 
% Create a table, |A|. 
A = table({'a' 'b' 'c' 'e' 'h'}',[1 2 3 11 17]',...
    'VariableNames',{'Key1' 'Var1'})  

%% 
% Create a table, |B|, with common values in the variable |Key1| between
% tables |A| and |B|, but also containing rows with values of |Key1| not
% present in |A|. 
B = table({'a','b','d','e'}',[4;5;6;7],...
    'VariableNames',{'Key1' 'Var2'})  

%% 
% Use the |outerjoin| function to create a new table, |C|, with data from
% tables |A| and |B|. Merge the key values into a single variable in the
% output table, |C|. 
C = outerjoin(A,B,'MergeKeys',true) 

%%
% Variables in table |C| that came from |A| contain null values in the rows
% that have no match from |B|. Similarly, variables in |C| that came from
% |B| contain null values in those rows that had no match from |A|.