function handle = load_system(sys)
%LOAD_SYSTEM Invisibly load a Simulink model.
%   LOAD_SYSTEM('SYS') loads the specified system without making the
%   model window visible.  If the specified system is 'built-in',
%   no action is taken.  The model name can be entered as a full
%   path, partial path or just the model name. The file extension
%   is optional.
%
%   See also OPEN_SYSTEM.

%   Copyright 1990-2021 The MathWorks, Inc.

if isnumeric(sys)
    % This is a pointless usage of load_system which needs to be maintained
    % for compatibility reasons.  All we're doing is validating the
    % supplied handle(s).
    handle = get_param(sys,'Handle');
else
    try
        handle = i_load_system(sys);
    catch E
        % Hide internal stack trace
        throw(E);
    end
end

if ~nargout
    clear handle;
end

end

function handle_ = i_load_system(sys_)

arguments
    sys_ string {mustBeText};
end

handle_ = zeros(size(sys_));
for k_=1:numel(sys_)
    if strcmp(sys_,'built-in')
        continue;
    end
    handle_(k_) = Simulink.internal.loadSystem(sys_(k_));
end

end

