Mikkel Astrup
Mikkel Astrup

Reputation: 415

Looping dataset matlab

Im doing some statistical analysis on multiply dataset but it seems really stupid hardcoding everything, so i was wondering if it's possible to make a loop for a dataset, the code i have is this:

dsA = dataset('XLSFile','RING 29 deg.xlsx','Sheet',7);
dsB = dataset('XLSFile','RING 29 deg.xlsx','Sheet',8);
dsC = dataset('XLSFile','RING 29 deg.xlsx','Sheet',9);
dsD = dataset('XLSFile','RING 29 deg.xlsx','Sheet',10);
dsE = dataset('XLSFile','RING 29 deg.xlsx','Sheet',11);
dsX = dataset('XLSFile','RING 29 deg.xlsx','Sheet',12);
dsY = dataset('XLSFile','RING 29 deg.xlsx','Sheet',13);

%Testing differences in median after 0,5 sex for A
[p,t,stats_A_1] = kruskalwallis(dsA.x0_5Sec,dsA.Code_1);
title('Differences in median after 0,5 sec for Concentration A')
print(gcf, '-dpdf', 'A_0,5_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_1);
title('Differences in median after 0,5 sec for Concentration A')
[nms num2cell(m)]


%Testing differences in median after 1 sex for A
[p,t,stats_A_2] = kruskalwallis(dsA.x1Sec,dsA.Code_1);
title('Differences in median after 1 sec for Concentration A')
print(gcf, '-dpdf', '73_1_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_2);
title('Differences in median after 1 sec for Concentration A')
[nms num2cell(m)]


%Testing differences in median after 1,5 sex for A
[p,t,stats_A_3] = kruskalwallis(dsA.x1_5Sec,dsA.Code_1);
title('Differences in median after 1,5 sec for Concentration A')
print(gcf, '-dpdf', '73_1,5_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_3);
title('Differences in median after 1,5 sec for Concentration A')
[nms num2cell(m)]


%Testing differences in median after 2 sex for A
[p,t,stats_A_4] = kruskalwallis(dsA.x2Sec,dsA.Code_1);
title('Differences in median after 2 sec for Concentration A')
print(gcf, '-dpdf', 'A_2_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_4);
title('Differences in median after 2 sec for Concentration A')
[nms num2cell(m)]


%Testing differences in median after 2,5 sex for A
[p,t,stats_A_5] = kruskalwallis(dsA.x2_5Sec,dsA.Code_1);
title('Differences in median after 2,5 sec for Concentration A')
print(gcf, '-dpdf', 'A_2,5_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_5);
title('Differences in median after 2,5 sec for Concentration A')
[nms num2cell(m)]


%Testing differences in median after 3 sex for A
[p,t,stats_A_6] = kruskalwallis(dA.x3Sec,dA.Code_1);
title('Differences in median after 3 sec for Concentration A')
print(gcf, '-dpdf', 'A_3_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_6);
title('Differences in median after 3 sec for Concentration A')

And i need to do that to dataset A to Y, and hardcoding that just seems stupid... But i have tried to make a loop like i do when im working with images but i can't make it work when i try with datasets, does anyone have an idea of how to do that? Have a nice day

Upvotes: 0

Views: 58

Answers (1)

Daniel
Daniel

Reputation: 36720

You have difficulties to write a loop because you are using variable names like stats_A_3 or dsA which have the index encoded into their variable name. To develop a loop version you have to refactor the code and remove such hard-coded indices. Start with two "iterations" like below and modify your code to have everything which differs between the iterations dependent on the variable index. You are finished when you have modified your code in a way that both blocks are identical but still do the same as previously.

%first iteration
index=1
t=index/2
dsA = dataset('XLSFile','RING 29 deg.xlsx','Sheet',7);
%Testing differences in median after 0,5 sex for A
[p,t,stats_A_1] = kruskalwallis(dsA.x0_5Sec,dsA.Code_1);
title('Differences in median after 0,5 sec for Concentration A')
print(gcf, '-dpdf', 'A_0,5_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_1);
title('Differences in median after 0,5 sec for Concentration A')
[nms num2cell(m)]
%second iteration
index=2
t=index/2
dsB = dataset('XLSFile','RING 29 deg.xlsx','Sheet',8);
%Testing differences in median after 1 sex for A
[p,t,stats_A_2] = kruskalwallis(dsA.x1Sec,dsA.Code_1);
title('Differences in median after 1 sec for Concentration A')
print(gcf, '-dpdf', '73_1_sec.pdf');
figure;
[c,m,h,nms] = multcompare(stats_A_2);
title('Differences in median after 1 sec for Concentration A')
[nms num2cell(m)]

Some tools you will need to achieve it (Check the documentation if you don't know them):

  • a cell array which replaces the stats_A_1 to stats_A_6 matrices
  • sprintf to generate your file names
  • dynamic field names

Upvotes: 1

Related Questions