
function comp=mergeComp(comp,newComp,idx)
% if there is an overlap between comp and newComp in terms of component names,
% merge them, i.e., copy the parameters (usually physics contants) to the one in newComp
cmnames=string(extractfield(comp,'name'));%unique
if length(cmnames)>length(unique(cmnames))
    disp('There are duplicate component names in comp')
    return
end
newcmnames=string(extractfield(newComp,'name'));%unique
if length(newcmnames)>length(unique(newcmnames))
    disp('There are duplicate component names in newComp')
    return
end
% Merge based on variable names and component names 
[~,idx_old,idx_new]=intersect(cmnames,newcmnames,'stable');
for i=1:length(idx_old)
    [~,idx_oldVar,idx_newVar]=intersect(comp(idx_old(i)).vars(:,idx.var),newComp(idx_new(i)).vars(:,idx.var),'stable');
    newComp(idx_new(i)).vars(idx_newVar,idx.val)=comp(idx_old(i)).vars(idx_oldVar,idx.val); %copy the parameters to the one in newComp
    comp(idx_old(i)).vars=newComp(idx_new(i)).vars; % replace the one in comp with the one in newComp 
end
% Append different new components to comp
newN=1:length(newcmnames);
Lia = ~ismember(newN,idx_new);
comp=[comp,newComp(Lia)];
end
