- Author:
- WeiweiAi <wai484@aucklanduni.ac.nz>
- Date:
- 2022-04-21 11:56:18+12:00
- Desc:
- Further automate the mapping process
- Permanent Source URI:
- https://models.fieldml.org/workspace/6bc/rawfile/4f2e241af0f1dbffded70d0aa6b23e73e5f06b01/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);
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);
inits = Vars(:,5);
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';
% 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 infor 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 repetitions of variable names')
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