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

2 views (last 30 days)
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.

Accepted Answer

Walter Roberson
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
Qusay Al Chatti
Qusay Al Chatti on 20 Apr 2021
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 on 20 Apr 2021
You should initialize Frame Components Results as a 2d cell array.

Sign in to comment.

More Answers (1)

Edric Ellis
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)
out{1,1} = result: 2 out{2,1} = result: 3 out{1,2} = result: 3 out{2,2} = result: 4
  2 Comments
Qusay Al Chatti
Qusay Al Chatti on 19 Apr 2021
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 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

Sign in to comment.

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!