How can i use the output of function after parfor loop?

조회 수: 2 (최근 30일)
Qusay Al Chatti
Qusay Al Chatti 2021년 4월 19일
댓글: Edric Ellis 2021년 4월 20일
The following is my code
parfor ii =1:10
for jj = 1:length(DynamicAnalysis.Name)
chk=1;
GroupName=char(GlobalGroupDescrption(ii));
fprintf('Working on Extracting Frame Component Forces for %s Analysis %s\n',GroupName,DynamicAnalysis.Name{jj});
FrameComponentResults=GetFrameComponentResults(DynamicAnalysis.Name{jj},DynamicAnalysis.Number(jj),DynamicAnalysis.TimeStep(jj),DIR{jj},ii,GroupData,PerformGroup,GroupComponentType,EleComponentType,EleComponentRepeat,SortedEleCompType,SortedEleCompRepeat);
%%%%%
end
end
As outlined, GetFrameComponentResults is a function. The output of the function is FrameComponentResults, which is a struct array. How can I prevent the FrameComponentResults from being considered as a temporary variable so that I store its content following each "jj" for-loop iteration.

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 19일
parfor ii =1:10
for jj = 1:length(DynamicAnalysis.Name)
chk=1;
GroupName=char(GlobalGroupDescrption(ii));
fprintf('Working on Extracting Frame Component Forces for %s Analysis %s\n',GroupName,DynamicAnalysis.Name{jj});
FrameComponentResultsj{jj} = GetFrameComponentResults(DynamicAnalysis.Name{jj},DynamicAnalysis.Number(jj),DynamicAnalysis.TimeStep(jj),DIR{jj},ii,GroupData,PerformGroup,GroupComponentType,EleComponentType,EleComponentRepeat,SortedEleCompType,SortedEleCompRepeat);
%%%%%
end
FrameComponentResults(ii,:) = FrameComponentResultsj;
end
  댓글 수: 4
Qusay Al Chatti
Qusay Al Chatti 2021년 4월 20일
Many thanks, it worked except with this minor modification
FrameComponentResults{ii} = FrameComponentResultsj;
Otherwise, it was giveing me error "Conversion to double from cell is not possible."
Walter Roberson
Walter Roberson 2021년 4월 20일
You should initialize Frame Components Results as a 2d cell array.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Edric Ellis
Edric Ellis 2021년 4월 19일
You need to output the result into some sort of "sliced variable". Basically this means outputting an array where one of the subscripts is the parfor loop variable. For example:
parfor ii = 1:2
for jj = 1:2
% Here, 'out' is a sliced output variable.
out{ii,jj} = struct('result', ii + jj);
end
end
format compact, celldisp(out)
out{1,1} = result: 2 out{2,1} = result: 3 out{1,2} = result: 3 out{2,2} = result: 4
  댓글 수: 2
Qusay Al Chatti
Qusay Al Chatti 2021년 4월 19일
Thanks for your response. In my case, I can do
out{ii,jj} = struct('result', FrameComponentResults);
where FrameComponentResults is struct array and is the output of my function. But, by doing so, matlab is giving me error as follow
Error: The variable out in a parfor cannot be classified.
I don't get the same error if I try
out{ii,1} = struct('result', FrameComponentResults);
But i can't have jj=1. Is there a way to classify variable out properly?
Thanks
Edric Ellis
Edric Ellis 2021년 4월 20일
Hm, you might be tripping over the rule about the bounds of the nested for loop. It might work to do this:
Ni = length(DynamicAnalysis.Name)
parfor ii = 1:10
for jj = 1:Ni
out{ii,jj} = ...
end
end

댓글을 달려면 로그인하십시오.

카테고리

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