%% Initialize Simulation Using Previous Simulation Results
%
% Load the G protein model.

sbioloadproject gprotein.sbproj;

%
% Check the initial amounts of species.

m1.Species

%
% Select only the species as the states to log for simulation.

cs = getconfigset(m1);
allspecies = sbioselect(m1,'Type','species');
cs.RuntimeOptions.StatesToLog = allspecies;

%
% Simulate the model.

sd = sbiosimulate(m1);

%
% Use the Data property of the SimData object sd to get the states at the final time point. The Data property is an m-by-n matrix, where m is the number of time steps and n is the number of quantities logged.

finalData = sd.Data(end,:);

%
% Use the DataInfo property to get the names of logged states.

info = sd.DataInfo;

%
% Loop through the species and set their initial values.

numSpecies = length(info);
vObj = addvariant(m1,'initCond');
for i = 1:numSpecies
    addcontent(vObj,{'species',info{i}.Name,'Value',finalData(i)});
end
commit(vObj,m1);

%
% Verify the species initial amounts.

m1.Species

% Copyright 2019 The MathWorks, Inc.