function setdatasrc(h, srcobj, varargin)
%

%   Author(s): James G. Owen, Rong Chen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:35:19 $

% TO DO: Consider using parent method: setContainer(this,Variable,ValueArrayContainer)

% Create the timeSeriesSrcArray storage object for the data src
storageObj = tsdata.timeseriesSrcArray;

% Identify the desired storage array
timesrc = false;
if nargin==3
    [var, ValueArrayPos] = findvar(h,varargin{1});
    % Is the the time vector data source
	if strcmpi(varargin{1},'time')
        timesrc = true;
	end
else % Default data source applies to "Data" 
    [var, ValueArrayPos] = findvar(h,'Data');
end

% Copy properties from the former @timeseriesArray
set(storageObj,'Variable',h.Data_(ValueArrayPos).Variable, 'Metadata', ...
    h.Data_(ValueArrayPos).metadata, 'Storage',srcobj);

% If this is a time data source, force the timemetadata to referesh 
% so that the start and end times reflect the source. 
if timesrc
    t = storageObj.getArray;
    if ~issorted(t) || length(unique(t))<length(t)
        error('Data sources cannot be used for time vectors which are not strictly increasing')
    end

    if h.Grid_.Length==0
        h.Grid_.Length = length(t);
    elseif length(t)~=h.Grid_.Length
        error('Mismatch between data source size and grid size')
    end
    % Assign the timemetadata length
%     storageObj.metadata.Accesstoken_ = '@timeseries/setdatasrc';
%     storageObj.metadata.Length = length(t);
%     storageObj.metadata.Accesstoken_ = '';
    % Assign the samplesize
    storageObj.SampleSize = [1 1];
end

% Replace the former @timeSeriesArray
storageArray = h.Data_;
storageArray(ValueArrayPos) = storageObj;
h.Data_ = storageArray;
if h.timeinfo.length==0
    try
       s = srcobj.size;
    catch
       error('This syntax is invalid for data sources that so not support getsize')
    end
    if s(1)>1
        h.Time = (0:(s(1)-1))';
    else
        h.time = (0:(s(end)-1))';
    end
end