function setSlice(h,slice,X,varargin)

% TO DO: This method does not yet allow setslice for slices which are not a
% single contiguous block 

% Slice is specified as a cell array containing a vector of
% indices or logicals

slice = slice{:};
if length(size(slice))>2 || min(size(slice))>1
   error('Can only slice into a single column')
end
if islogical(slice)
    slice = find(slice);
end
if max(slice)>(h.EndRow-h.StartRow+1) || min(slice)<1
    error('Slice vector out of bounds')
end
minslice = min(slice);
thisStartRow = h.StartRow+minslice-1;
thisEndRow = h.StartRow+max(slice)-1;
slice = slice-minslice+1;

% Argument checking
cols = floor(h.Column);
if isempty(cols)
    return
end
if any(cols<1)
    error('Column positions must be greater than 0')
end
if size(X,2)~=length(cols)
    error('The number of data columns do not match the number of data source columns')
end
[cols J] = sort(cols);
X = X(:,J);
cellX = num2cell(X);
cellX(isnan(X))={[]};
X=cellX;

% Parse column vector into contiguous intervals and extract by block
k = 1;
dataRange = {};
while k+1<=length(cols)
  lastCol = cols(k);
  lastk = k;
  while k+1<=length(cols) && cols(k+1)-cols(k)==1
      k = k+1;
  end
  cellblock = localParseColNumbers(lastCol,cols(k),thisStartRow,thisEndRow);
  set(h.comhandle.Range(cellblock));
  h.comhandle.Range(cellblock).Value = X(:,lastk:k);
  k =  k+1;
end

% If there is one isolated column remaining
if k==length(cols)
  cellblock = localParseColNumbers(cols(k),cols(k),thisStartRow,thisEndRow);
  h.comhandle.Range(cellblock).Value = X(:,k);
end




function cellblock = localParseColNumbers(startcol,endcol,startrow,endrow)

% Parses column and row vertices of a rectangle into the form 'XX#:YY#'

startcolstr = char('A'+rem(startcol,26)-1);
if startcol>26
   startcolstr = [char(floor(startcol/26)-1+'A')  startcolstr];
end
endcolstr = char('A'+rem(endcol,26)-1);
if endcol>26
   endcolstr = [char(floor(endcol/26)-1+'A')  endcolstr];
end
cellblock = [startcolstr sprintf('%d',startrow) ':' endcolstr sprintf('%d',endrow)];








