Use structures in a parfor loop
8 views (last 30 days)
Show older comments
Hello everyone.
I have a problem using structures in a parfor loop. I would like to save my output of the parfor loop in a structure called "Output".
if strcmpi(String.Frequency_Filter,'Yes')
WaitMessage = parfor_wait(NN,'Waitbar',true);
if strcmpi(String.Output_type, 'displacement') || strcmpi(String.Differentiation_Variable, 'Frequency')
parfor ii=1:NN
if strcmpi(String.DOF,'X')
Output.u_G_Interp(ii,:)=bandpass(Output.u_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'Y')
Output.v_G_Interp(ii,:)=bandpass(Output.v_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'Z')
Output.w_G_Interp(ii,:)=bandpass(Output.w_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'XYZ')
Output.u_G_Interp(ii,:)=bandpass(Output.u_G_Interp(ii,:),B,fs); Output.v_G_Interp(ii,:)=bandpass(Output.v_G_Interp(ii,:),B,fs); w_G_Interp(ii,:)=bandpass(Output.w_G_Interp(ii,:),B,fs);
end
WaitMessage.Send;
end
end
end
Unfortunately (like in the figure below) it is not possible to use directly structures in a parfor loop and I cannot use a temporary variable inside the parfor loop and after writing it inside my structure because the if that i will use in the parfor loop depends on a defind String in the main of my code and not all the variables exists. For example if i have String.DOF='Z' just the w_G_Interp variable exists and I get an error in writing the u_G_Interp variable beacause it doesn't exist.
if strcmpi(String.Frequency_Filter,'Yes')
WaitMessage = parfor_wait(NN,'Waitbar',true);
if strcmpi(String.Output_type, 'displacement') || strcmpi(String.Differentiation_Variable, 'Frequency')
parfor ii=1:NN
if strcmpi(String.DOF,'X')
u_G_Interp(ii,:)=bandpass(Output.u_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'Y')
v_G_Interp(ii,:)=bandpass(Output.v_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'Z')
w_G_Interp(ii,:)=bandpass(Output.w_G_Interp(ii,:),B,fs);
elseif strcmpi(String.DOF,'XYZ')
u_G_Interp(ii,:)=bandpass(u_G_Interp(ii,:),B,fs); v_G_Interp(ii,:)=bandpass(Output.v_G_Interp(ii,:),B,fs); w_G_Interp(ii,:)=bandpass(Output.w_G_Interp(ii,:),B,fs);
end
WaitMessage.Send;
end
end
end
Output.u_G_Interp=u_G_Interp;
Output.v_G_Interp=v_G_Interp;
Output.w_G_Interp=w_G_Interp;
I hope the question is clear and thank you in advance for the reply.
0 Comments
Answers (1)
Deepak
on 11 Dec 2024
We can resolve this by using temporary variables to handle parallel processing within a “parfor” loop. Initialize these variables outside the loop and use a “switch-case” or conditional statements inside the loop to process only the relevant data based on conditions (e.g., “String.DOF”). After the loop, assign the results back to the structure for the fields that were processed, avoiding errors from non-existent fields.
This approach avoids attempting to access or modify non-existent fields during parallel execution, thereby preventing runtime errors and ensuring that the structure is updated correctly.
Below is the MATLAB code to achieve the same:
if strcmpi(String.Frequency_Filter, 'Yes')
WaitMessage = parfor_wait(NN, 'Waitbar', true);
% Initialize temporary variables
u_G_Interp = [];
v_G_Interp = [];
w_G_Interp = [];
if strcmpi(String.Output_type, 'displacement') || strcmpi(String.Differentiation_Variable, 'Frequency')
parfor ii = 1:NN
switch String.DOF
case 'X'
u_G_Interp(ii, :) = bandpass(Output.u_G_Interp(ii, :), B, fs);
case 'Y'
v_G_Interp(ii, :) = bandpass(Output.v_G_Interp(ii, :), B, fs);
case 'Z'
w_G_Interp(ii, :) = bandpass(Output.w_G_Interp(ii, :), B, fs);
case 'XYZ'
u_G_Interp(ii, :) = bandpass(Output.u_G_Interp(ii, :), B, fs);
v_G_Interp(ii, :) = bandpass(Output.v_G_Interp(ii, :), B, fs);
w_G_Interp(ii, :) = bandpass(Output.w_G_Interp(ii, :), B, fs);
end
WaitMessage.Send;
end
end
% Assign results back to the structure
if any(strcmpi(String.DOF, {'X', 'XYZ'}))
Output.u_G_Interp = u_G_Interp;
end
if any(strcmpi(String.DOF, {'Y', 'XYZ'}))
Output.v_G_Interp = v_G_Interp;
end
if any(strcmpi(String.DOF, {'Z', 'XYZ'}))
Output.w_G_Interp = w_G_Interp;
end
end
Please find attached the documentation of functions used for reference:
switch block: www.mathworks.com/help/matlab/ref/switch.html
I hope this assists in resolving the issue.
0 Comments
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!