Creating a Parfor loop for multiple function evaluation

조회 수: 1 (최근 30일)
H R
H R 2017년 5월 29일
답변: Walter Roberson 2017년 5월 29일
Hello. I want to use parfor to reduce the calculation time for a loop I have by evaluating 6 functions at the same time. Could you please help me out with this:
The general form of the loop in my m file is:
x=rand(100,20);
global fidr_m;
fidr_m= fopen('C:/results.txt','w'); %%???
for i=1:100
Y(i)=func(x(i,:))
end
and my function (func) includes some lines to generate a unique text file from a base file depending on the looping variable (i=1:100) and run a very costly function WW.
function y = func(X)
if isempty(count)
count = 1;
else
count = count +1;
end
BasePath='C:/MyComputer/';
BaseName='Comp';
File = strcat(Basename, BaseName, num2str(count),'.txt');
y=WW(File,X);
XX=[X,y];
global fidr_m;
if fidr_m==-1
error('can not open the file');
end
fprintf(fidr_m, '%f\t', XX);
fprintf(fidr_m,'\n');

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 29일
You do not define count in the function before you use it.
global variables are not copied between workers.
You cannot use a worker to do i/o on a file that you open in a different worker.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by