Location: cellLib @ 0f94d0bdf02a / Scripts / readComp.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.physiomeproject.org/workspace/6bc/rawfile/0f94d0bdf02afd16ebe8651a92628c98334ddb1f/Scripts/readComp.m

function comp=readComp(filename,compname)
fid = fopen(filename);
comp={};
tline = fgetl(fid);
vars=[];
k = 0; % Counter for components.
while ischar(tline)
    compLocation = strfind(tline, '<component name=');
    if ~isempty(compLocation)
    temp_name=string(extractBetween(tline,'"','"'));
    end
    if ~isempty(compLocation)&& ~isempty(find(compname==temp_name,1))% only save the specified component
        k = k + 1;
        comp(k).name=string(extractBetween(tline,'"','"'));
        comp(k).children=[];        
    end
    compEnd=strfind(tline, '</component>');
    if ~isempty(compEnd) && ~isempty(find(compname==temp_name,1))
        comp(k).vars=vars;
        vars=[];
    end
    varLocation = strfind(tline, '<variable');
    if ~isempty(varLocation) && ~isempty(find(compname==temp_name,1))
        vname=string(extractBetween(tline,'name="','"'));
        unit=string(extractBetween(tline,'units="','"'));
        init=string(extractBetween(tline,'initial_value="','"'));
        pub=string(extractBetween(tline,'public_interface="','"'));
        priv=string(extractBetween(tline,'private_interface="','"'));
        ctg="para"; %default category is parameter
        if isempty(init)
            init="none";
        else
            if (pub=="out")
                ctg=pub;
            else
                ctg="interVar";
            end
        end
        if isempty(pub)
            pub="none";
            ctg="interVar";
        else
            if pub=="out" %cannot be para; if "in", could be para or input, need to manually check later
                ctg=pub;
            end
        end
        if isempty(priv)
            priv="none";
        end
        
        var=[vname,init,unit,init,pub,priv,"",ctg];
        vars=[vars;var];
    end
    tline = fgetl(fid);
end
fclose(fid);
end