function [ xdata, ydata, endTime ] = getDataForMeasurements( this, scopeBlock, displays, displayIndex, varargin)
%GETDATAFORMEASUREMENTS Summary of this function goes here
%   Detailed explanation goes here
% Return null if we have a screen message on the visual

%   Copyright 2014-2016 The MathWorks, Inc.

if scopeBlock.UnifiedScope.screenMsg
    xdata=[];ydata=[];
elseif ~scopeBlock.IsDataThinningOn
    data = getXY( this, true );
    if isempty( data )
        xdata = [];
        ydata = [];
        endTime = NaN;
        return
    end
    % Remove start time information from xdata.  output should reflect what
    % is seen in the display, usually [0, TimeSpan]
    xdata = data( 1, : );
    ydata = data( 2, : );
elseif nargin > 4
    % This condition is hit when measurements asks for data in a given range.
    assert(this.Real && ~this.FrameBased);
    sla = this.getSLA();
    hfig = ancestor(sla,'Figure');
    tsBlk = hfig.UserData.Specification.Block;
    data = tsBlk.getSimDataInRange(this,varargin{1},varargin{2});
    xdata = data( 1, : );
    ydata = data( 2, : );
else
    sampleTimes = [];    
    frameBased = this.FrameBased;
    frameRates = [];
    plotMagPhase = [];
    for eachDisp = displays
        sampleTimes = [sampleTimes eachDisp{1}.SampleTimes];
        frameRates = [frameRates eachDisp{1}.LengthOfChannels'];
        plotMagPhase = [plotMagPhase eachDisp{1}.PlotMagPhase]; %#ok<*AGROW>
    end
    allSimData = scopeBlock.getSimData;
    % For floating scope, when we start removing signals, we delete logvar
    % from C++. The time and data value becomes emtpy and we should make
    % the whole datastructure null.
    if scopeBlock.Floating
        if isa(allSimData,'Simulink.SimulationData.Dataset')
            values = allSimData.getElement(1).Values;
            if isstruct(values)
                timeseriesArray = Simulink.scopes.getFlatArrayFromStruct(values);
                timeElement = timeseriesArray(1).Time;
            else
                timeElement = values.Time;
            end
            if (isempty(timeElement))
                allSimData = [];
            end
        elseif isstruct(allSimData)
            if(isempty(allSimData.time))
                allSimData = [];
            end           
        end
    end
    [structureOffset,lineOffset] = scopeBlock.getPortAndLineID(this);   
    [xdata,ydata] = getSimDataForLineImpl(scopeBlock,displayIndex, allSimData,...
        structureOffset,lineOffset,frameBased, frameRates, sampleTimes, this.Real, this.Phase, true);
    
end

if isempty( xdata )
    endTime = NaN;
    return
else
    endTime = xdata(end);
    xdata = xdata - displays{ 1 }.StartTime;
end

end

