Location: cellLib @ 0f94d0bdf02a / Scripts / mergeComp.m

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-05-16 13:04:54+12:00
Desc:
Add a merge script and update accordingly
Permanent Source URI:
https://models.cellml.org/workspace/6bc/rawfile/0f94d0bdf02afd16ebe8651a92628c98334ddb1f/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).vars(:,idx.var),newComp(idx_new).vars(:,idx.var),'stable');
    newComp(idx_new).vars(idx_newVar,idx.val)=comp(idx_old).vars(idx_oldVar,idx.val); %copy the parameters to the one in newComp
    comp(idx_old).vars=newComp(idx_new).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