- Author:
- WeiweiAi <wai484@aucklanduni.ac.nz>
- Date:
- 2022-08-02 19:00:05+12:00
- Desc:
- Add BG paras_conv.m
- Permanent Source URI:
- https://models.fieldml.org/workspace/6bc/rawfile/4d87e24edb3731b3379a42ab58e90c4c1c0a7f7b/Scripts/mergeComp.m
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