Printing figures within parfor

The following is a simple example of printing figures within a loop:


outdir = 'C:\fillinadirectory
A = [0 1];
N = 2;

%---if you print from within a regular for loop the figures come
%---out as expected (i.e. with 150 dpi)
for i=1:N
  %---make figure---
  figure(1); clf;
  plot(A,A,'b-');
  title(['Iteration ', num2str(i), ' plotted with for']);
  %---print figure---
  fnout = [outdir, '\for', num2str(i,'%.2d'), '.jpg'];
  print('-djpeg','-r150',fnout);
end

The problem is if you enclose the same code in a parfor the figures do appear, but they have low resolution (72 dpi) even though I have specified that they should have 150 dpi

matlabpool open 2
parfor (i=1:N)
  %---make figure---
  figure(1); clf;
  plot(A,A,'b-');
  title(['Iteration ', num2str(i), ' plotted with parfor']);
  %---print figure---
  fnout = [outdir, '\parfor', num2str(i,'%.2d'), '.jpg'];
  print('-djpeg','-r150',fnout);
end
matlabpool close

The output from the for loop has higher resolution than the output of the parfor loop.

I got some help on this issue from the matlab support team: "The observed behavior is because workers are started with the "-noFigureWindows" option. Much of the printing code behaves differently in this case, and this appears to an example. You can confirm this by starting matlab like "matlab -noFigureWindows" and running the [above] for-loop example."

The tech support people suggested a workaround, which is to specifiy the size of the figure window before printing, for example:

for i=1:N
    %---make figure---
    figure(1); clf;

    plot(A,A,'b-');


    %IMPT: instructs MATLAB to print using the same size as that shown on the screen
    set(1,'PaperPositionMode','auto');
    %find all axes in figure(1)
    axes_h = findall(1,'type','axes');
    %set font unit to normalized so that the labels resize with the figure
    set(axes_h,'fontunits','normalized');
    %Resize figure Window
    set(1, 'Position',[0 0 1700 1200]);    %Resize figure Window

    title(['Iteration ', num2str(i), ' plotted with for']);

    %---print figure---
    fnout = [outdir, '\wa3_for', num2str(i,'%.2d'), '.jpg'];
    print('-djpeg',fnout);
    
    close 1;    %close figure(1)

end

This approach works fine from within a for loop. You do get a high resolution figure from this code. Unfortunately, if you run this within a parfor loop (see code below) the axes do not scale with the figure size, so even though the figure itself is larger, it is mosly margin.

matlabpool open 2
parfor (i=1:N)
  %---make figure---
  figure(1); clf;
  %---the fix---
  %IMPT: instructs MATLAB to print using the same size as that shown on the screen
  set(1,'PaperPositionMode','auto');  %IMPT: instructs MATLAB to print using the
  %find all axes in figure(1)
  axes_h = findall(1,'type','axes'); 
  %set font unit to normalized so that the labels resize with the figure
  set(axes_h,'fontunits','normalized'); 
  %Resize figure Window
  set(1, 'Position',[0 0 1700 1200]);    
  %---the plot---
  plot(A,A,'b-');
  title(['Iteration ', num2str(i), ' plotted with parfor']);
  %---print figure---
  fnout = [outdir, '\parforwa3', num2str(i,'%.2d'), '.jpg'];
  print('-djpeg','-r150',fnout);
end
matlabpool close

At this point there does not seem to be a way to control the figure size when printing within parfor. They have registered this as a bug/ enhancement request so maybe in a future version of PCT this will be fixed. (hopefully!?)

As a final note, one of the engineers asked me why I would want to print on the workers. The PCT model, he said, was to compute on the workers and then collect the results to print on the client. I suggested two scenarios where printing on a worker would be useful, but if anyone can think of other cases where printing on the worker is useful I would love to hear about it - maybe it would help build the case for matlab making a fix to this problem a higher priority.


Update June 9,2008

I received a final email from tech support on this issue. Bad news = no solution for now. Good news = maybe in the future? Email copied here:

Hello Art,

Thank you for your patience. Unfortunately, the worker machines and the Parallel Computing Toolbox in general is really only meant for data processing. For your specific purpose, the only way around the limitation would be to generate the figures and save them to FIG files like so:

saveas(gcf,fnout_fig,'fig');

where "fnout_fig" would be the filename for the resulting FIG file. Afterwards, you can then use the client MATLAB machine to open the FIG file then use the PRINT command to change the resolution like so:

open(fnout_fig)
print('-djpeg','-r150',fnout);

I hope this workaround is sufficient for your needs.

I also wanted to inform you that this is actually not a bug. The reason for this behavior is that when MATLAB is running as a worker, it defaults to using Ghostscript which ignores the -r option. For more information about this limitation, please consult the MATLAB help documentation by executing the following command in MATLAB:

doc print

However, I will be submitting an enhancement request for you instead.


Return to MATLAB PCT notes

Return to Art Gleason Home

Last Updated: May 2008