function out = subs_struct(expr, S)
    names = fieldnames(S);
    vals  = struct2cell(S);
    out = subs(expr, sym(names), [vals{:}]');

    % If fully numeric after substitution, convert to double
    if isempty(symvar(out))
        out = double(out);
    end

end