2014-11-14 208 views
0

我有一個循環的腳本經過幾個不同的值組合。該腳本使用值組合調用main,然後調用訪問不同值的parfor。以下是我的代碼的虛擬簡化。如果需要,我會提供完整的代碼。Matlab:在parfor循環中訪問函數輸入

循環:

a = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]; 
b = [5,10,15,20,25,30]; 
c = [0,1]; 

% Iterate over all possible combinations 
for p = 1:length(a) 
    for s = 1:length(b) 
     for e = 1:length(c) 
      main(p,s,e); clear all; 
     end 
    end 
end 

主營:

function main (p,s,e) 
    parfor k = 1:51 
     if(e) 
      display('Foobar'); 
     end 
    end 
end 

所以我基本上要在PARFOR循環來決定做什麼(例如,如何建立間隔等)與輸入的幫助參數。我不想編輯這個參數,只是閱讀和使用它們。

現在我得到以下錯誤:

An UndefinedFunction error was thrown on the workers for 'e'. This might be because the file containing 'e' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.

我不明白爲什麼這是行不通的。再次定義e,就像 e2 = e 也沒有幫助。

問候

編輯: 究竟似乎工作是當我傳遞的不變量循環直接到主,但實際使用數組像預期。 例如:

main(a(p),b(s),c(e)) 
+0

難道這是因爲你調用'main'後的'clear all'語句?嘗試評論它,看看它是否有效。 – am304 2014-11-14 14:45:25

+0

另外,如果可能的話,在'main'函數之外設置'parfor'循環可能更好,並將調用傳遞給'parfor'循環內的'main'。 – am304 2014-11-14 14:46:51

+0

@ am304:不,這不應該是原因,並沒有幫助(只是試了一下)。由於循環腳本應該按順序執行,因此直到主調用完成後,全部清除都不應執行。 parfor應該保留在主要功能,我現在沒有看到另一種合理的方式。 – Xolair 2014-11-14 14:50:06

回答

0

我找到了解決方案。 我試圖將分配給變量的變量傳遞給main函數。這不可能。 現在我真正想要(而且做)的是將前面定義的數組中的值傳遞給主函數。

a = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]; 
b = [5,10,15,20,25,30]; 
c = [0,1]; 

% Iterate over all possible combinations 
for p = 1:length(a) 
    for s = 1:length(b) 
     for e = 1:length(c) 
      main(a(p),b(s),c(e)); clear all; 
     end 
    end 
end