Location: cellLib @ af1f8d8d26f3 / Scripts / mergeComp.m

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-06-07 10:38:19+12:00
Desc:
Add a simulation script without reset
Permanent Source URI:
https://models.physiomeproject.org/workspace/6bc/rawfile/af1f8d8d26f3cd9f85a1de11bd50811021c52f06/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