Location: cellLib @ e653d60e211e / Scripts / newComp.m

Author:
WeiweiAi <wai484@aucklanduni.ac.nz>
Date:
2022-04-13 16:28:32+12:00
Desc:
Add Matlab scripts to build cellML models
Permanent Source URI:
https://models.cellml.org/workspace/6bc/rawfile/e653d60e211e14e8a4b552af1bc280317474fe43/Scripts/newComp.m

function [comp,idx]=newComp(filename)
% Read data
opts = detectImportOptions(filename);
varTypes = {'string','string','string','string'};
varNames = opts.VariableNames ;
opts = setvartype(opts,varNames,varTypes);
Vars = readmatrix(filename,opts);
% Get basic components
IndexComp=[find(~ismissing(Vars(:,1)));length(Vars(:,1))+1];
ncomp=length(IndexComp); % # of components
% 
vars = Vars(:,2);
values=Vars(:,3);
units = Vars(:,4);
nvars=length(values);
% Categorize
vio=strcat('{pub: in}',strings(1,nvars))';
vctg=strcat('',strings(1,nvars))';
priv=strcat('none',strings(1,nvars))';
pub=strcat('none',strings(1,nvars))';
init=strcat('none',strings(1,nvars))';
% Intermediate vars
indexvar=(values=='var');
vio(indexvar)='';
vctg(indexvar)='interVar';
indexinit=(values=='init:');
vctg(indexinit)='interVar';
init(indexinit)='interVar';
indexi=find(indexinit);
for i=1:length(indexi)
    vio(indexi(i))=sprintf('{init: %s}',vars(indexi(i)-1));
    init(indexi(i))=sprintf('%s',vars(indexi(i)-1));
end
% Output vars
indexout=(values=='out');
vio(indexout)='{pub: out}';
vctg(indexout)='out';
pub(indexout)='out';
indexiout=(values=='init:out');
vctg(indexiout)='out';
pub(indexiout)='out';
indexi=find(indexiout);
for i=1:length(indexi)
    vio(indexi(i))=sprintf('{init: %s, pub: out}',vars(indexi(i)-1));
    init(indexi(i))=sprintf('%s',vars(indexi(i)-1));
end
% Input vars
indexin=(values=='in');
vctg(indexin)='in';
pub(indexin)='in';
% Paras and init
indexp=~(indexin|indexout|indexvar|indexinit|indexiout);
vctg(indexp)='para';
pub(indexp)='in';
% Add more infor to the Vars
Vars=[Vars,init,pub,priv,vio,vctg,vars];
% Write components
comp={};
for i=1:ncomp-1
    comp(i).name=string(Vars(IndexComp(i),1));
    comp(i).children='';
    indexrange=(IndexComp(i):IndexComp(i+1)-1);
    t.vars=(vars(indexrange));
    t.values=values(indexrange);
    t.units=(units(indexrange));
    t.vios=vio(indexrange);
    t.vctgs=vctg(indexrange);
    t.init=init(indexrange);
    t.priv=priv(indexrange);
    t.pub=pub(indexrange);    
    comp(i).vars=[t.vars,t.vars,t.values,t.units,t.init,t.pub,t.priv,t.vios,t.vctgs];    
end
idx.var=1;idx.alias=2;idx.val=3;idx.unit=4; idx.init=5;
idx.pub=6;idx.priv=7;idx.vio=8;idx.vctg=9;
end