To accelerate the plotting process of the “CC” structure, the Parallel Computing Toolbox can be used. Since each iteration of the loop is independent of the others (each iteration processes a different object in “CC” and plots it), this is a suitable case for parallelization.
NOTE: Manipulating figures and creating plots usually requires access to the main MATLAB thread, and direct plotting with parallel workers might not be efficient due to the graphical backend limitations. To overcome this limitation, all computationally intensive tasks (like calculating “isosurfaces”) should be performed in parallel, and then the results are collected to plot them in the main thread.
Here is the approach using the parallel for loop (parfor) in MATLAB:
- It must be ensured that a parallel loop is running. If MATLAB is not configured to automatically start a parallel pool when needed, the following code can be used to open a new pool:
pool = gcp;
- The “isosurface” calculations are done in parallel, followed by the plotting of the results in a single thread, as given below:
V = zeros([526, 526, 551]);
pin = CC.PixelIdxList{1, i};
[faces, verts] = isosurface(V, 0.5);
results{i} = struct('faces', faces, 'verts', verts, 'colorIndex', mod(i-1, 255)+1);
p = patch('Faces', results{i}.faces, 'Vertices', results{i}.verts);
p.FaceColor = colors(results{i}.colorIndex, :);
Hope it helps!