function [TestData] = DownloadProfile(dt,x)
result = 0;

if((length(x) < 2))
    result = -1;  %Time series too short
    return;
end

if(length(x) >= 209000)
    result = -2;  %Time Series too long
    return;
end

if(dt <= 0)
    result = -3;  %Invalid time-step size
    return
end

NumPoints = length(x);
t = dt*(0:NumPoints-1);
plot(t,x);
xlabel('time (s)');
ylabel('Position (in)');

rmc = ConnectToRMC;
%srv = actxserver('RMCLink.RMCLinkServer');
%rmc = srv.CreateEthernetLink(2,'134.89.12.240');
%rmc.Connect;

if(NumPoints <= 503)  %Download as single curve if short enough
    Status = 0;
    CurveFormat = 0;  %Single Curve
    NumberOfPoints = NumPoints;
    InterpolationOptions = 0;  %0 = Cubic Spline
    PointZeroValue = 0;  %0 = time
    Xinterval = dt;
    
    data = [Status CurveFormat NumberOfPoints InterpolationOptions PointZeroValue Xinterval x];
    rmc.WriteFFile(56,10,length(data),data);               %Download Curve Data
    
    %Issue CurveAdd Commmand
    cmd = [82 0 56*4096+10  2 0];
    rmc.WriteFFile(16,0,length(cmd),cmd);               %Send cmd to small command area
    
    while true
        status = rmc.ReadFFile(56,10,1);
        disp(['status = ' num2str(status)]);
        if(status == 3)
            break;
        end
    end
    
end

if(NumPoints > 503)  %Download as partial curves
    
    Status = 0;
    CurveFormat = 20;  %Partial sections of longer curve
    TotalLength = NumPoints+4;
    PointCount = NumPoints;
    InterpolationOptions = 0;  %0 = Cubic Spline
    PointZeroValue = 0;  %0 = time
    Xinterval = dt;
    
    PartLength = 507;
    StatusCheck = 2;
    TestData = {};
    i = 1;
    for PartOffset = 0:507:NumPoints
        
        if(PartOffset == 0)  %First part
          data = [Status CurveFormat PartOffset PartLength TotalLength PointCount InterpolationOptions PointZeroValue Xinterval x(PartOffset+1:PartOffset+1+PartLength-5)];
        else
          if(NumPoints-PartOffset <= PartLength)
              PartLength = NumPoints-PartOffset+4;  %Last Segment may be shorter
              StatusCheck = 3;
          end
          data = [Status CurveFormat PartOffset PartLength TotalLength x(PartOffset+1-4:PartOffset+1-5+PartLength)];
        end
        
        
        TestData{i} = data;
        i = i+1;
        rmc.WriteFFile(56,10,length(data),data);               %Download Curve Data
        
        %Issue CurveAdd Commmand
         cmd = [82 0 56*4096+10  2 0];
         rmc.WriteFFile(16,0,length(cmd),cmd);               %Send cmd to small command area
        
        
        while true
            status = rmc.ReadFFile(56,10,1);
            disp(['Downloaded Part ' num2str(i) ' Status =  ' num2str(status)]);
            if(status == StatusCheck)
                break;
            end
            
        end
        
    end
end








end

