Execute multiple functions in parfor

1 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by