runlength.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. function [ts_rl] = runlength(ts, ts_sh, plot_flag, time_axis, color_vec, y_offset, alphafill, alphaline)
  2. if nargin<8
  3. if nargin<7
  4. if nargin <6
  5. if nargin<5
  6. if nargin<4
  7. if nargin<3
  8. plot_flag=0;
  9. end
  10. if isvector(ts)
  11. time_axis=1:length(ts);
  12. else
  13. error('input1 needs to be vector');
  14. end
  15. end
  16. color_vec=[0 0 0];
  17. end
  18. y_offset=nan;
  19. end
  20. alphafill=.2;
  21. end
  22. alphaline=1;
  23. end
  24. % ts [Ntimepoints x 1] contains time series data
  25. % ts_sh [Ntps x Nshuffles] contains shuffled data
  26. if isrow(ts)
  27. ts=ts';
  28. end
  29. if ismatrix(ts_sh) && any(size(ts_sh)==length(ts))
  30. if size(ts_sh,2)==length(ts)
  31. ts_sh=ts_sh';
  32. end
  33. else
  34. error('input2 is not correct in size');
  35. end
  36. ts_p95=prctile(ts_sh,95,2);
  37. ts_ccs=countconsecutiveones(ts_sh>ts_p95);
  38. ts_ccs_p95=prctile(rmnans(ts_ccs(:)),95);
  39. if isnan(ts_ccs_p95); ts_ccs_p95=0; end
  40. [~,~,~,ts_rlen]=countconsecutiveones((ts>ts_p95),0,ts_ccs_p95);
  41. ts_rl=zeros(1,length(ts)); ts_rl(ts_rlen==0)=nan;
  42. if (plot_flag)
  43. plotmsem(time_axis,ts,color_vec); hold on;
  44. plotperc(time_axis,ts_sh,[5 95],color_vec,alphafill);
  45. gyl=get(gca,'YLim');
  46. if isnan(y_offset); y_offset=min(gyl)+0.01.*diff(gyl); end
  47. plot(time_axis,y_offset+ts_rl,'color',[color_vec alphaline],'linewidth',5); hold on;
  48. ylim(gyl);
  49. end