Add a function to merge two tables that might have different columns. Both sets of columns are preserved
This commit is contained in:
1 parent
f61e1113b0
commit
3f0d9be72a
1 file changed
+49
@@ -0,0 +1,49 @@
|
||||
function Table = outerjoinRowToTable(Table,TableRowToBeAdded)
|
||||
%outerjoinRowToTable Adds one row to a table. Variables that are missing in
|
||||
%either the table or the row are added with empty values {' '} for cells
|
||||
%and NaN for doubles. Works with tables that contain both cells and
|
||||
%doubles.
|
||||
%
|
||||
% * Strings in the table are not yet supported.
|
||||
% * The row that is added needs to be formated as table
|
||||
% * Adds only one row at a time%
|
||||
%
|
||||
% T = outerjoinRowToTable(TableToAddTo,RowToBeAdded)
|
||||
%
|
||||
% tmpTableToBeAdded = table({tmpCombined(:,1)},{tmpCombined(:,2)},{'660eV, m50, 092022'},{'#288,#289,#290.ibw'}, ...
|
||||
% 'VariableNames',["bindingEnergy_eV","spectrum_countsNorm","sample","name"]);
|
||||
% SpectrumList = outerjoinRowToTable(SpectrumList,tmpTableToBeAdded);
|
||||
%
|
||||
% Thorsten, December 2023
|
||||
|
||||
% Original Table has more variables
|
||||
if ~all(ismember(Table.Properties.VariableNames,TableRowToBeAdded.Properties.VariableNames))
|
||||
tmpMissingVariable = ~ismember(Table.Properties.VariableNames,TableRowToBeAdded.Properties.VariableNames);
|
||||
tmpVariablesToBeAdded = Table.Properties.VariableNames(tmpMissingVariable);
|
||||
for ii = 1:size(tmpVariablesToBeAdded,2)
|
||||
if matches(class(Table.(tmpVariablesToBeAdded{ii})),'double')
|
||||
tmpContentToBeAdded = NaN(1,size(Table.(tmpVariablesToBeAdded{ii}),2));
|
||||
TableRowToBeAdded = addvars(TableRowToBeAdded,tmpContentToBeAdded,'NewVariableNames',tmpVariablesToBeAdded{ii});
|
||||
elseif matches(class(Table.(tmpVariablesToBeAdded{ii})),'cell')
|
||||
tmpContentToBeAdded = {' '};
|
||||
TableRowToBeAdded = addvars(TableRowToBeAdded,tmpContentToBeAdded,'NewVariableNames',tmpVariablesToBeAdded{ii});
|
||||
end
|
||||
end
|
||||
end
|
||||
% Table to be added has more variables
|
||||
if ~all(ismember(TableRowToBeAdded.Properties.VariableNames,Table.Properties.VariableNames))
|
||||
tmpMissingVariable = ~ismember(TableRowToBeAdded.Properties.VariableNames,Table.Properties.VariableNames);
|
||||
tmpVariablesToBeAdded = TableRowToBeAdded.Properties.VariableNames(tmpMissingVariable);
|
||||
for ii = 1:size(tmpVariablesToBeAdded,2)
|
||||
if matches(class(TableRowToBeAdded.(tmpVariablesToBeAdded{ii})),'double')
|
||||
tmpContentToBeAdded = NaN(size(Table,1),size(TableRowToBeAdded.(tmpVariablesToBeAdded{ii}),2));
|
||||
Table = addvars(Table,tmpContentToBeAdded,'NewVariableNames',tmpVariablesToBeAdded{ii});
|
||||
elseif matches(class(TableRowToBeAdded.(tmpVariablesToBeAdded{ii})),'cell')
|
||||
tmpContentToBeAdded = repmat({' '},size(Table,1),1);
|
||||
Table = addvars(Table,tmpContentToBeAdded,'NewVariableNames',tmpVariablesToBeAdded{ii});
|
||||
end
|
||||
end
|
||||
end
|
||||
% Add row to table
|
||||
Table = [Table;TableRowToBeAdded];
|
||||
end
|
||||
Reference in new issue
Block a user