function [barcodeGen_grouped,barcodeGen_trimmed,allIdx,barcodeGen_excluded] = barcode_bargroup_cleaner(barcodeGen, cGenAll)
    % Collect all indices to remove
    allIdx = [];

    for i = 1:numel(cGenAll)
        if isfield(cGenAll{i}, 'idx')
            allIdx = [allIdx, cGenAll{i}.idx];
        end
    end

    % Make indices unique and sorted for safety
    allIdx = unique(allIdx);

    % Remove those indices from barcodeGen
    keepMask = true(1, numel(barcodeGen));
    keepMask(allIdx) = false;

    % Apply the mask and return reindexed cell array
    barcodeGen_trimmed = barcodeGen(logical(keepMask));
    barcodeGen_excluded = barcodeGen(~logical(keepMask));
    barcodeGen_grouped = [barcodeGen_trimmed cGenAll];
end
