diff --git a/utils/outerjoinRowToTable.m b/utils/outerjoinRowToTable.m index 1d37b4c..74d9e8a 100644 --- a/utils/outerjoinRowToTable.m +++ b/utils/outerjoinRowToTable.m @@ -5,8 +5,9 @@ function Table = outerjoinRowToTable(Table,TableRowToBeAdded) %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. Update 2024: Adds several rows, that is tables +% * The row that is added needs to be formatted as table +% * Update 2024: Adds several rows, that is tables +% * Update 2026: Handles peakfitting columns with different size. % % T = outerjoinRowToTable(TableToAddTo,RowToBeAdded) % @@ -50,6 +51,35 @@ if ~all(ismember(TableRowToBeAdded.Properties.VariableNames,Table.Properties.Var end end end +% Reconcile column widths for peak-fit columns. +% These numeric columns can have variable width (one entry per peak), +% so a width mismatch is expected when spectra have different numbers +% of peaks. Pad the narrower side with NaN. +variableWidthColumns = {'peakArea', 'peakPosition', 'peakFWHM', ... + 'peakBackground', 'peakEta', 'peakShirleyK'}; + +for ii = 1:numel(variableWidthColumns) + varName = variableWidthColumns{ii}; + + if ~ismember(varName, Table.Properties.VariableNames) || ... + ~ismember(varName, TableRowToBeAdded.Properties.VariableNames) + continue + end + + widthT = size(Table.(varName), 2); + widthR = size(TableRowToBeAdded.(varName), 2); + + if widthT < widthR + Table.(varName) = [Table.(varName), ... + NaN(height(Table), widthR - widthT)]; + elseif widthR < widthT + TableRowToBeAdded.(varName) = [TableRowToBeAdded.(varName), ... + NaN(height(TableRowToBeAdded), widthT - widthR)]; + end +end + +% Ensure matching column order before concatenation +TableRowToBeAdded = TableRowToBeAdded(:, Table.Properties.VariableNames); % Add row to table Table = [Table;TableRowToBeAdded]; end \ No newline at end of file