Updated the HowDoI: Added how to merge tables with different columns into one.

This commit is contained in:
bartels-rausch committed 2025-12-07 11:41:12 +01:00
1 parent bbfab45787
commit 38a62bf560
1 file changed
+91
@@ -129,3 +129,94 @@ xlabel('Auger-Meitner binding energy [eV]');
dataObjs = findobj(gcf,'-property','YData');
dataObjs(2).Color = [184 184 185]/255;
dataObjs(3).Color = [58 162 190]/255;
%% ... combine two tables with common and different columns (variables) into one?
%
% This example shows how a helper function (outerjoinRowToTable) can
% append rows from one table to another even if the tables do not start
% with exactly the same set of variables (columns).[web:59]
for ii=1
%% Create simple table 1
%
% --- Table 1: one row, six columns of different types --------------------
%
% Types requested:
% 1) character -> stored as cell containing a char vector
% 2) string -> string scalar
% 3) cell array -> heterogeneous container
% 4) cell -> cell holding a scalar
% 5) array -> numeric vector
% 6) number -> scalar double
%
% MATLAB tables can hold mixed data types column-wise as long as each
% column has the same number of rows.[web:59]
%
% For "character": use a 1x1 cell that contains a char vector
charCol = {'name1'}; % cell containing a character vector
stringCol = "hello"; % string scalar
cellArrayCol = {1 2 3}; % 1x3 cell array
cellCol = {42}; % 1x1 cell, holding a number
arrayCol = 1:5; % numeric row vector
numberCol = 3.14; % scalar double
Table1 = table(charCol, stringCol, cellArrayCol, cellCol, arrayCol, numberCol, ...
'VariableNames', {'CharCol','StringCol','CellArrayCol','CellCol','ArrayCol','NumberCol'});
%% Create Table 2
%
% --- Table 2: one row, with a different set of variables -----------------
%
% Table2 shares the variable CharCol with Table1, but also has a new
% variable OneNumber that does not exist in Table1.
%
% When we combine the tables, CharCol will become a column with two rows
% (name1 and name2). The new variable OneNumber will be added to Table1,
% and the missing value for the first row will be filled with a numeric
% placeholder (NaN).
oneNumber = 99; % scalar double
charCol = {'name2'}; % cell containing a character vector
Table2 = table(oneNumber, charCol, ...
'VariableNames', {'OneNumber', 'CharCol'});
disp(Table1);
disp(Table2);
%% Combine both into one
%
% outerjoinRowToTable does the following:
% 1) It compares the VariableNames of both tables.
% - Columns with the same name (here CharCol) are aligned and will
% end up as a single column with multiple rows.
% 2) For variables that only exist in one table:
% - If a variable is missing in the incoming rows, a new column is
% added to that smaller table and filled with placeholder values
% (NaN for numeric types, ' ' or " " for text/cell/string types),
% so that the column exists for all rows.
% - This ensures both tables have the same set of columns before
% concatenation.
% 3) After the variable sets are harmonized, the rows are concatenated
% with standard vertical stacking, so the result is one table where:
% - Shared columns (CharCol) contain data from both input tables
% in separate rows.
% - Columns that were unique to one input (e.g. OneNumber or
% ArrayCol) show data where available and placeholder values where
% that column did not exist originally.[web:59]
Table3 = outerjoinRowToTable(Table1,Table2);
disp(Table3)
% In a real workflow, this pattern allows you to successively append
% FileList tables from multiple beamtimes, even if new metadata
% columns were introduced later. Older rows are back-filled with
% placeholder values for new variables, while shared columns accumulate
% additional rows representing more files / experiments.
%
% Example:
% tmpData = load('../beamtimes/20220828BeamTimeIceNappIss/FileList.mat');
% tmpData2 = load('../beamtimes/20211216BeamTimeLjSim/FileList.mat');
% FileList = outerjoinRowToTable(tmpData.FileList,tmpData2.FileList);
end