plotmsem.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function plms= plotmsem(xaxis, yvalues, colorvs, alpha, linespec, linewidth, mflag)
  2. %plotmsem plots mean +/- sem with shaded, continuous error bars
  3. if nargin <7
  4. if nargin < 6
  5. if nargin < 5
  6. if nargin < 4
  7. alpha=.1;
  8. end
  9. linespec='-';
  10. end
  11. linewidth=2;
  12. end
  13. mflag=1; % plot mean, mflag=2 plot median
  14. end
  15. if nargin>=3 && ischar(colorvs) && contains('kbgcrmyw',colorvs(1))
  16. color=rem(floor((strfind('kbgcrmyw',colorvs(1))-1)*[.25 .5 1]),2);
  17. if length(colorvs)>1
  18. linespec=colorvs(2:end);
  19. end
  20. elseif nargin>=3 && isa(colorvs,'double')
  21. color=colorvs(1:3);
  22. else
  23. color=[0 0 0];
  24. end
  25. if ~isvector(xaxis)
  26. error('x-axis vector has to be 1D.');
  27. elseif iscolumn(xaxis)
  28. xaxis=xaxis';
  29. end
  30. if ~ismatrix(yvalues)
  31. error('y-values matrix has to be 2D.');
  32. end
  33. if ~any(size(yvalues)==length(xaxis))
  34. error('x-axis and y-values must have equal size along 1D (at least).');
  35. end
  36. % force xaxis and yvalues to have same number of rows (not columns)
  37. if size(yvalues,2)~=length(xaxis)
  38. yvalues=yvalues';
  39. end
  40. % mean and sem are computed along rows
  41. if mflag==1
  42. myv=nanmean(yvalues,1);
  43. elseif mflag==2
  44. myv=nanmedian(yvalues,1);
  45. end
  46. semyv=nansem(yvalues,1);
  47. plms.semfill=fill([xaxis xaxis(end:-1:1)]',[myv-semyv myv(end:-1:1)+semyv(end:-1:1)]',color,'facealpha',alpha,'linestyle','none'); hold on;
  48. plms.meanline=plot(xaxis,myv,linespec,'Color',color,'Linewidth',linewidth);
  49. end