How to use print in a parfor

3 views (last 30 days)
meghannmarie
meghannmarie on 23 Aug 2021
Commented: meghannmarie on 23 Aug 2021
Whenever I am using print inside a parfor, I get some all black images. How do I fix this?
Here is a snippet of code and I attached the variables figs and output_files (I had to make them way smaller to upload).
Thanks!!
load('variables.mat')
% output resolution and format
outputDPI = '-r900';
outputFmt = '-dpng';
parfor f = 1:numel(figs)
figure1 = figs{f};
output_file = output_files{f};
if ~isempty(figure1)
opengl software
set(groot, 'DefaultFigureRenderer', 'painters');
print(figure1, output_file, outputFmt, outputDPI);
close(figure1);
end %if ~isempty(figure1)
end %for f = 1:numel(figs)
  2 Comments
meghannmarie
meghannmarie on 23 Aug 2021
It actually makes an all black graphic or a graphic with the legend all messed up (see attached graphics). If you try to run the code in a parfor and a for loop and compare outputs, the graphics are different. I had to make the fig cell array smaller because it exceeded the 5mb limit on the size of file upload on this website. Try this code where I just repeated the cell array 20 times and you should see the bad graphics (I also took the open gl out).
load('variables.mat')
% output resolution and format
outputDPI = '-r900';
outputFmt = '-dpng';
figs = repmat(figs,1,20);
output_files = cell(size(figs));
for n = 1:numel(figs)
output_files{n} = ['Wind_Rose_' num2str(n,'%02.0f')];
end
parfor f = 1:numel(figs)
figure1 = figs{f};
output_file = output_files{f};
if ~isempty(figure1)
% opengl software
set(groot, 'DefaultFigureRenderer', 'painters');
print(figure1, output_file, outputFmt, outputDPI);
close(figure1);
end %if ~isempty(figure1)
end %for f = 1:numel(figs)

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by