Execute multiple functions in parfor

1 回表示 (過去 30 日間)
endeavour90
endeavour90 2012 年 4 月 10 日
Hi, Currently I have several functions, named function1.m, function2.m, function3.m , ..., function10.m. Each function is independent each other. I would like to run the all the functions in one execution
currently, my code is like this, it runs the functions one by one.
for i = 1 : 10
result = eval(sprintf('function%d.m',i));
end
I would like to know is there a way to rewrite the code in parfor instead of for, as I know that eval does not work in parfor. Thank you

採用された回答

Walter Roberson
Walter Roberson 2012 年 4 月 10 日
functions = {@function1, @function2, @function3, @function4, @function5, @function6, @function7, @function8, @function9, @function10};
parfor i = 1 : length(functions)
functions{i}()
end
Your current code is going to be a problem, as it will attempt to access the field named "m" in the return value of function "function1"
Any time you use eval(), you are probably doing something wrong. (There are a few times that it is needed, but not often.)
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 4 月 10 日
functions = cell(10,1);
for i = 1 : 10
functions{i} = str2func(sprintf('function%d',i));
end
parfor i = 1 : length(functions)
functions{i}()
end
endeavour90
endeavour90 2012 年 4 月 10 日
Thanks a lot :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by