function [inportUsage, allUnused, anyUnused] = getInportUsage(sldvData)
%

%   Copyright 2010 The MathWorks, Inc.
   
    inportInfo = sldvData.AnalysisInformation.InputPortInfo;
    numInports = length(inportInfo);
    inportUsage = cell(1,numInports);
    allUnused = [];
    anyUnused = false;
    for idx=1:numInports
        isPortBusArray = false;
        if iscell(inportInfo{idx}) && isfield(inportInfo{idx}{1}, 'Dimensions') && ...
                                            any(inportInfo{idx}{1}.Dimensions ~= 1)
            isPortBusArray = true;
        end
        inportUsage{idx} = portUsage(inportInfo{idx}, isPortBusArray);

        if isempty(allUnused)
            allUnused = all(~inportUsage{idx});
        else
            allUnused = allUnused && all(~inportUsage{idx});
        end
        anyUnused = anyUnused || any(~inportUsage{idx});
    end
    if isempty(allUnused)
        allUnused = false;
    end
end

function inportUsage = portUsage(inportInfo, isBusArray, inportUsage)
    if nargin<3
        inportUsage = [];
    end
    
    if isstruct(inportInfo)
        inportUsage(end+1) = inportInfo.Used;
    else
        % iscell(inportInfo), i.e., inportInfo corresponds to bus or bus array
        if isBusArray
            numElems = 1;
            if isfield(inportInfo{1}, 'Dimensions')
                numElems = prod(inportInfo{1}.Dimensions);
            end

            for i=1:numElems % Yes, i is not used in the loop!
                inportUsage = portUsage(inportInfo, false, inportUsage);
            end
        else
            for i=2:length(inportInfo)
                portInfo = inportInfo{i};
                isChildBusArray = false;
                if iscell(portInfo) && isfield(portInfo{1}, 'Dimensions') && any(portInfo{1}.Dimensions ~= 1)
                    isChildBusArray = true;
                end

                inportUsage = portUsage(portInfo, isChildBusArray, inportUsage);
            end
        end
    end
end