- Author:
- WeiweiAi <wai484@aucklanduni.ac.nz>
- Date:
- 2023-04-04 15:51:18+12:00
- Desc:
- Add a steady state example
- Permanent Source URI:
- https://models.physiomeproject.org/workspace/6bc/rawfile/a7940fcacd455a09bdb7beac0da91eca79cf1adb/Scripts/newComp.m
function [comp,idx]=newComp(filename)
%% Read data
opts = detectImportOptions(filename);
varTypes = {'string','string','string','string','string'};
varNames = opts.VariableNames ;
opts = setvartype(opts,varNames,varTypes);
opts.DataLines=[2,Inf];
Vars = readmatrix(filename,opts);
%% Get basic components
IndexComp=[find(~ismissing(Vars(:,1)));length(Vars(:,1))+1];
ncomp=length(IndexComp); % # of components
%% Get variables
vars = Vars(:,2);
values=Vars(:,3);
units = Vars(:,4);
inits = Vars(:,5);
nvars=length(values);
% Categorize the variables
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';
% Init values
indexinit=(inits~='none');
vio(indexinit)=sprintf('{init: %s}',inits(indexinit));
init(indexinit)=inits(indexinit);
% Output vars
indexout=(values=='out');
vio(indexout)='{pub: out}';
vctg(indexout)='out';
pub(indexout)='out';
% Input vars
indexin=(values=='in');
vctg(indexin)='in';
pub(indexin)='in';
% Paras and init
indexp=~(indexin|indexout|indexvar|indexinit);
vctg(indexp)='para';
pub(indexp)='in';
% Add more information to the Vars
Vars=[Vars,init,pub,priv,vio,vctg,vars];
%% Write components
comp={};
idx.var=1;idx.val=2;idx.unit=3; idx.init=4;
idx.pub=5;idx.priv=6;idx.vio=7;idx.vctg=8;
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));
if length(t.vars)>length(unique(t.vars))
disp('There are duplicate variables within a component')
return
end
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.values,t.units,t.init,t.pub,t.priv,t.vios,t.vctgs];
end
end