function ii = NaN_runs(X,lim)

% find indices for runs of NaN longer than a specified limit
% in an input vector.

jj = find(isnan(X)); % indices for all NaN values
temp = []; ii = [];
for kk = 1:length(jj)-1
    if jj(kk+1) - jj(kk) ==1
        % accumulate indices that differ by 1
        if isempty(temp)
            temp = jj(kk:kk+1);
        else
            temp = [temp;jj(kk+1)];
        end
    else
        if length(temp)>=lim
            ii = [ii;temp];
            temp= [];
        else
            temp = [];
        end
    end
end
% catch any remaining indices at end of loop
if length(temp)>=lim
    ii = [ii;temp];
else

end

