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