function isLoaded = bdIsLoaded(bdnames)
%bdIsLoaded - Returns whether or not a block diagram is in memory
%
%   isLoaded = bdIsLoaded(bdnames)
%
% bdnames can be a string or a cell array of strings.  All strings
% must be valid block diagram names.  It is an error to supply a
% path to a block or subsystem.
%
% isLoaded is a logical array with one entry for each block diagram name.
%
% Examples:
%  bdIsLoaded('sf_car') % returns logical scalar
%  bdIsLoaded({'sf_car','vdp'}) % returns 1*2 logical array

%   Copyright 2007-2016 The MathWorks, Inc.

if ischar(bdnames)
    bdnames = {bdnames};
elseif iscell(bdnames)
    bdnames = cellstr(bdnames);
elseif isstring(bdnames)
    bdnames = cellstr(bdnames);
else
    DAStudio.error('Simulink:utility:InvalidBlockDiagramName');
end

valid = cellfun(@i_allowableName,bdnames);
if any(~valid)
    DAStudio.error('Simulink:utility:InvalidBlockDiagramName');
end
is_distinct_name = contains(bdnames,'@');

isLoaded = false(size(bdnames));
if isSimulinkStarted
    all_loaded = find_system('SearchDepth',0);
    isLoaded(~is_distinct_name) = slcellmember(bdnames(~is_distinct_name),all_loaded);
    if any(is_distinct_name)
        distinct_names = get_param(all_loaded,'DistinctName');
        isLoaded(is_distinct_name) = slcellmember(bdnames(is_distinct_name),distinct_names);
    end
end

end

function allowed = i_allowableName(n)
    allowed = false;
    if isempty(n)
        return;
    end
    if n(1)=='@'
        allowed = true;
        return;
    end
    allowed = isvarname(n);
end


