function window=mpc_getrv(window,signal,t1,t2,addone)
% MPC_GETRV Retrieves reference/measured disturbance signals for preview
%
% window=mpc_getrv(window,signal,t1,t2,addone) defines a column vector
% "window" where window=[signal(:,t1+1); ... ;signal(:,t2+1)]. If matrix
% signal does not have enough columns, the last column is repeated. When
% "addone" is "1", add 1 after each signal(:,t1+k).  The time axis in the
% signal matrix is column. t1 and t2 are in time steps.

%   Authors: A. Bemporad
%   Copyright 1986-2011 The MathWorks, Inc.

%#codegen

ONE = ones('like',signal);
[n,len]=size(signal);
m=n+addone;
k=t1;
while k<=t2,
    if k<=len-1,
        % Grab signal corresponding to time k
        for j=1:n,
            window((k-t1)*m+j)=signal(j,k+1);
        end
    else
        % Repeat last sample
        for j=1:n,
            window((k-t1)*m+j)=signal(j,len);
        end
    end
    if addone,
        window((k-t1)*m+n+1)=ONE;
    end
    k=k+1;
end
