saving graphs by parfor command cause to empty figure

Hi,
I want to save 12 graphs created in other functions with using the "parfor" command. For this I collect all the handles in a single function named "SaveGraphs". Since the command "print" takes a long of time, I decided to apply the "parfor". My code looks as following
function SaveGraphs(f1,f2,...,f12)
graphs=[f1;f2;...f12];
filePath={'path1';'path2;...'path12'};
SizeVec= matrix 12 X 2 with appropriated sizes
for k=1:length(graphs)
set(figure(graphs(k)),'PaperUnits','inches','PaperPosition',[0 0 SizeVec(k,:)])
print(graphs(k),'-dpng','-r1',filePath{k})
end
parfor k=1:length(graphs)
print(figure(graphs(k)),'-dpng','-r1',filePath{k})
end
The graphs are saved but they are empty. Any hint at a solution?
If I change the "parfor" to "for", it works.
Thanks in advance for helping

 采纳的回答

If I've understood correctly, you are creating the figures at the client and trying to call print from the workers. This will not work because the workers are separate MATLAB processes, and cannot access the figures defined at the client. To do something like this, you need the workers to create the figures and then call print.

5 个评论

Hi, Thanks for answer. I would be save all graphs in centralized manner. May be I can to try the "spmd"?
Yes, you could create the figures and then call print inside an spmd block, or even inside a parfor loop.
Meanwhile, I discovered something interesting. When I create the figures, I write (for instance I taking the f1)
f1 = figure(51);
set(f1, 'Visible', 'off')
set(f1,'name' , name).
In this case f1=51. But if I do
spmd
f1 = figure(51);
set(f1, 'Visible', 'off')
set(f1,'name' , name)
end
the f1 looks as following
Lab 1: class = double, size = [1 1]
Lab 2: class = double, size = [1 1]
Lab 3: class = double, size = [1 1]
Lab 4: class = double, size = [1 1].
In addition, f1(1)=f1(2)=f1(3)=f1(4)=[51]. The class of f1 is Composite.
Therefor the Q. now, if I can to use the "parfor" without "spmd" ? Because if I understand true, now all workers see the graphs
The another Q. if in any case I need to use the "spmd" (in the function saveAllgraphs), can you give me a little example for my function?
I'm not entirely sure what you're after, but how about something like this:
parfor idx = 1:10
f = gcf();
clf(f);
subplot(1,2,1);
imagesc(magic(idx));
subplot(1,2,2);
peaks(10*idx);
print('-dpng', sprintf('fig%d.png', idx));
end
In this case it not helps me because I create graphs out of function. In any case thanks for answer

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by