function newvalue = mpc_chkstate(type,value,n,offset)
%MPC_CHKSTATE Check validity of extended state vector in engineering units
%and return offset-free values
%
% type:         'Plant' or 'Disturbance' or 'Noise' or 'LastMove'
% value:        statess in engineering units
% n:            dimension
% off:          offsets
%
% newvalue:     offset-free states

%   Author: A. Bemporad
%   Copyright 1986-2007 The MathWorks, Inc.

if isempty(value)
    % Dist or Noise
    newvalue = zeros(n,1); % Default
else
    if ~isa(value,'double') || ~all(isfinite(value(:)))
        ctrlMsgUtils.error('MPC:computation:InvalidStateValue');
    else
        [nx,mx] = size(value);
        if nx*mx~=n 
            ctrlMsgUtils.error('MPC:computation:InvalidDim',['State.' type],n);                
        end
        newvalue = value(:)-offset;
    end
end
