function [m, conNew, obj] = updateAll(m, con, obj, T, useParams, useICs, useModelICs)
%UPDATEALL Update Kronecker Bio structures when parameters change
%
%   [m, con, obj] = updateAll(m, con, obj, T, useParams, useICs, useModelICs)
%
%   This function performs the often needed task of updating m, con, and
%   obj whenever the active parameters T change.

% Constants
nx = m.nX;
nCon = size(con, 1);
nObj = size(obj, 2);
nTk = nnz(useParams);
nTx = nnz(useICs);
nT = nTk + nTx;

% Update parameter sets
k = m.p;
k(useParams) = T(1:nTk);
if useModelICs
    x0 = m.ic;
    x0(useICs) = T(nTk+1:nT);
else
    x0 = zeros(nx,nCon);
    for iCon = 1:nCon
        x0(:,iCon) = con(iCon).ic;
    end
    x0(useICs) = T(nTk+1:nT);
end

% Update model
if useModelICs
    m = m.update(k, x0);
else
    m = m.update(k, m.ic);
end

% Update experimental conditions
if ~isempty(con)
    conNew = Uzero(nCon);
    if useModelICs
        for iCon = 1:nCon
            conNew(iCon) = pastestruct(Uzero(m), con(iCon).update(m));
        end
    else
        for iCon = 1:nCon
            conNew(iCon) = pastestruct(Uzero(m), con(iCon).update(m, x0(:,iCon)));
        end
    end
end

% Update objective functions
if ~isempty(obj)
    obj = refreshObj(m,obj);
end
