function out = setData(h, newval)
%SETDATA Assigns data into quality ValueArray
%
%   SETDATA is used to assign data into the quality ValueArray. It checks
%   that the stored quality codes match the conversion table specified in
%   the qualmetadata object and convertis to int8 values prior to storage.
%
%   Author(s): J.G. Owen
%   Copyright 1986-2003 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:34:41 $

% Sets data to the ValueArray data property if the "data" property

if size(newval,2)>1
    error('Quality variable must be a single numeric column vector')
end
if ~all(ismember(newval,h.Code)) && ~all(isnan(newval))
    %warning('One or more quality codes are not specified in qualityInfo')
end

% Cast quality codes to integers and check that this does not currupt the
% data
oldval=newval;
newval(~isnan(newval))=int8(newval(~isnan(newval)));
if ~isempty(newval) && any(abs(oldval-double(newval))>eps(255))
    error('qualmetadata:setData:allint','Quality codes must all be in the range -128 to 127')
end

% workaround: UDD bug causing segV on non-Windows platforms
if ~isempty(newval)
    out = newval;
else
    out = [];
end