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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by