How can i use the output of function after parfor loop?
2 views (last 30 days)
Show older comments
Qusay Al Chatti
on 19 Apr 2021
Commented: Edric Ellis
on 20 Apr 2021
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.
0 Comments
Accepted Answer
Walter Roberson
on 19 Apr 2021
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 Comments
More Answers (1)
Edric Ellis
on 19 Apr 2021
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)
2 Comments
Edric Ellis
on 20 Apr 2021
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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!