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

2 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

Edric Ellis
Edric Ellis el 19 de Abr. de 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 comentarios
Qusay Al Chatti
Qusay Al Chatti el 19 de Abr. de 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 el 20 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by