MATLAB: Error using histogram in parlor-loop

histogramParallel Computing Toolboxparfortransparency

Hey,
I have trouble using histogram in a parfor-loop. Below is a minimal-working-example of my problem:
parfor idx = 1:2
x = randn(1,50*idx);
f = figure()
histogram(x,'Normalization','pdf')
drawnow
end
This gives me the response:
Error using histogram (line 140)
Transparency violation error.
See Parallel Computing Toolbox documentation about Transparency
Error in untitled6 (line 3)
parfor idx = 1:2
I get the error with both R2018a and R2018b
Best regards,

Best Answer

Unfortunately, histogram tries to extract the name of the input variable, and this causes the transparency violation. You can work around this problem by hiding the call to histogram inside an anonymous function, like this:
histFcn = @(x) histogram(x, 'Normalization', 'pdf');
parfor idx = 1:2
x = rand(1, 50*idx);
histFcn(x);
end