2016-03-01 92 views
0

Im做的乘法數據集的一些統計分析,但它似乎很愚蠢硬編碼的一切,所以我想知道如果有可能使一個循環的數據集,代碼我已經是這樣的:循環的數據集MATLAB

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') 

我需要做到這一點,數據集A到Y,硬編碼,只是似乎愚蠢...但我已經嘗試做一個循環,就像我在做圖像處理但我不能讓它工作,當我嘗試與數據集,有沒有人有如何做到這一點的想法? 祝您有美好的一天

回答

1

由於您使用的變量名稱如stats_A_3dsA的索引編碼爲其變量名,因此難以編寫循環。要開發循環版本,您必須重構代碼並刪除這些硬編碼索引。從下面的兩個「迭代」開始,修改代碼以使迭代之間的所有內容都取決於變量index。當你修改代碼時,你完成了兩個塊都相同但仍然與之前一樣的方式。

%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)] 

你將需要實現它(查看文檔,如果你不認識他們)一些工具:

  • 單元陣列,它取代了stats_A_1stats_A_6矩陣
  • sprintf產生您的文件名
  • dynamic field names
+1

我同意結構更簡單,如ds(1).data加ds(1).stats,並對每個數據集進行迭代。 –

+0

@ R.Bergamote:你說得對,統計數據中'struct'是更好的選擇。我沒有閱讀「kruskalwallis」的文檔,只是選擇了一個存儲任何內容的「單元」。 – Daniel