function tgaxUpdatePlot(hGrid,txPositions,rxPositions,activeTx,activeRx,mask,txChannels,rxChannels,titlestr)
%tgaxDropNodes Update the plot with node positions and links
%
%   This is an example helper function.

%   Copyright 2019 The MathWorks, Inc.

% Focus on figure 
figure(hGrid.fh);

% Make nodes visible
hGrid.sh1.Visible = 'on';
hGrid.sh2.Visible = 'on';

% Update node colors to show inactive/active state
numTx = size(txPositions,1);
numRx = size(rxPositions,1);
cdata = repmat([0.7 0.7 0.7],[numTx,1]);
cdata(activeTx,:) = repmat([228,26,28]/255,[sum(activeTx),1]);
hGrid.sh1.CData = cdata;
cdata = repmat([0.7 0.7 0.7],[numRx,1]);
cdata(activeRx,:) = repmat([55,126,184]/255,[sum(activeRx),1]);
hGrid.sh2.CData = cdata;

NumChannels = numel(unique(txChannels));
for ichan = 1:NumChannels
    txChanIdx = txChannels==ichan;
    rxChanIdx = rxChannels==ichan;

    % Update the node positions
    hGrid.sh1.XData(txChanIdx) = txPositions(txChanIdx,1);
    hGrid.sh1.YData(txChanIdx) = txPositions(txChanIdx,2);
    hGrid.sh1.ZData(txChanIdx) = txPositions(txChanIdx,3);

    hGrid.sh2.XData(rxChanIdx) = rxPositions(rxChanIdx,1);
    hGrid.sh2.YData(rxChanIdx) = rxPositions(rxChanIdx,2);
    hGrid.sh2.ZData(rxChanIdx) = rxPositions(rxChanIdx,3);

    numTx = size(txPositions(txChanIdx,:),1);
    numRx = size(rxPositions(rxChanIdx,:),1);

    % Update Links
    % Set inactive nodes positions to NaN for next part (so links don't show)
    txPositionsPlot = txPositions;
    rxPositionsPlot = rxPositions;
    txPositionsPlot(~(activeTx&txChanIdx),:) = NaN;
    rxPositionsPlot(~(activeRx&rxChanIdx),:) = NaN;

    txX = repmat(txPositionsPlot(txChanIdx,1),[1,numRx]);
    rxX = repmat(rxPositionsPlot(rxChanIdx,1),[1,numTx])';
    X = arrayfun(@(x,y,s) s.*[x,y,NaN],txX,rxX,mask(txChanIdx,rxChanIdx),'UniformOutput',false);
    x = cell2mat(X(:))';

    txY = repmat(txPositionsPlot(txChanIdx,2),[1,numRx]);
    rxY = repmat(rxPositionsPlot(rxChanIdx,2),[1,numTx])';
    Y = arrayfun(@(x,y,s) s.*[x,y,NaN],txY,rxY,mask(txChanIdx,rxChanIdx),'UniformOutput',false);
    y = cell2mat(Y(:))';

    txZ = repmat(txPositionsPlot(txChanIdx,3),[1,numRx]);
    rxZ = repmat(rxPositionsPlot(rxChanIdx,3),[1,numTx])';
    Z = arrayfun(@(x,y,s) s.*[x,y,NaN],txZ,rxZ,mask(txChanIdx,rxChanIdx),'UniformOutput',false);
    z = cell2mat(Z(:))';

    % Update interfering links (transparent lines, line handle lh)
    hGrid.lh(ichan).XData = x(:);
    hGrid.lh(ichan).YData = y(:);
    hGrid.lh(ichan).ZData = z(:);

    % Update links of interest (solid lines, line handle lh2)
    x = [repmat(txPositionsPlot(txChanIdx,1),[numRx/numTx,1]),rxPositionsPlot(rxChanIdx,1),NaN(numRx,1)]';
    y = [repmat(txPositionsPlot(txChanIdx,2),[numRx/numTx,1]),rxPositionsPlot(rxChanIdx,2),NaN(numRx,1)]';
    z = [repmat(txPositionsPlot(txChanIdx,3),[numRx/numTx,1]),rxPositionsPlot(rxChanIdx,3),NaN(numRx,1)]';
    
    hGrid.lh2(ichan).XData = x(:);
    hGrid.lh2(ichan).YData = y(:);
    hGrid.lh2(ichan).ZData = z(:);
end

if nargin>8
    title(hGrid.fh.CurrentAxes,titlestr);
end

% Update plot
drawnow

end