% CELLVAR Calculate variance for all elements of a cell array
% s = cellvar(cellarray)

function s = cellvar(cellarray)

if length(size(cellarray))>2
   error('This only works for cell arrays up to 2D');
end

if iscell(cellarray{1,1})
    for i=1:size(cellarray,1)
        for j=1:size(cellarray,2)
            s{i,j} = cellvar(cellarray{i,j});
        end
    end
    return;
end

for i=1:size(cellarray,1)
    for j=1:size(cellarray,2)
        thevar = var(cellarray{i,j});
        if length(thevar)==1
            s(i,j) = thevar;
        else
            s{i,j} = thevar;
        end
    end
end
