I have a very expensive loop that I'm trying to parallelize, and part of this loop involves updating an entry in a 4D array inside a matfile (I must save results to disk and access them through a matfile pointer due to RAM limitations). However, I get an error that says that the matfile pointer variable cannot be classified. As an illustrative example of what I'm trying to do, consider the code below:
testOut = ; % create variable to try and updatesave('TestFile.mat', 'testOut', '-v7.3'); % save variable into accessible matfileFileOut = matfile('TestFile.mat','Writable',true); % Set up pointer to matfilenrows = 200; ncols = 200; nplanes = 32; nvolumes = 10; % Set up dimensions of 4D arrayFileOut.testOut = single(zeros(nrows,ncols,nplanes,nvolumes)); % Set initial size of variable in matfileparfor i = 1:nvolumesFileOut.testOut(:,:,:,i) = i; % artificial example, point is that I want to update variable using fourth dimension indexend
The error this code would report is:
Error: The variable FileOut in a parfor cannot be classified.See Parallel for Loops in MATLAB, "Overview".
Basically, the loop is performing an independent calculation on a 3D volume image each time, and each resulting volume image is saved in a 4D array using the fourth dimension to mark volume image number. However, I often have thousands of these images, and so the 4D array must be saved to disk and accessed from a matfile to avoid overloading the RAM. I'd like to adapt my code to use parfor, but I can't figure out how to get parfor to play nicely with the matfile pointer. Can anyone help me out here, please? I understand that sliced variables must be used with parfor, and that variables of the form I'm using here are not allowed, but I can't figure out a solution…