n = 200;
mx = -40.0;
nperm = 1000;
alpha = 0.05;

%% (h) repeat for various means of the y-data set:
mys = [-40.1, -40.2, -40.5];
for k=1:length(mys)

  %% (a) generate data:
  my = mys(k);
  x = randn(n, 1) + mx;
  y = randn(n, 1) + my;

  %% (d), (e) permutation test:
  [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);

  %% (c) difference of means:
  fprintf('\nmean x = %.1fmV, mean y = %.1fmV\n', mx, my);
  fprintf('  difference of means = %.2fmV\n', md);

  %% (g) significance:
  fprintf('  difference of means at 5%% significance = %.2fmV\n', dq);
  if md >= dq
      fprintf('  --> measured difference of means is significant\n');
  else
      fprintf('  --> measured difference of means is not significant\n');
  end

  %% (b), (f) plot histograms of data and pdf of differences:
  meandiffplot(x, y, md, ds, dq, k, length(mys));

  subplot(length(mys), 2, k*2-1);
  title(sprintf('mx=%.1fmV, my=%.1fmV', mx, my))
end

savefigpdf(gcf, 'meandiffsignificance.pdf', 12, 10);