MATLAB: Reference a matrix which changes in size throughout parfor loop

MATLABParallel Computing Toolboxparforstruct

I am attempting to use the parallel computing toolbox (with Matlab R2013b) to speed up some data analysis, specifically by implementing a parfor loop. I have a variable, data, which is obtained through an I/O function, and then subsequently referenced throughout the parfor loop.
For example:
clear all; close all; clc;
N = 2;
parfor loopcount = 1:N
data(loopcount).rawdata = IOfunc(loopcount,inputinfo(loopcount))
end
The reason for using a structure like this is that the size of "rawdata" varies for different values of loopcount. This type of structure allowed for me to build up "data" with "rawdata"'s of varying size. The above code works for my needs.
The problem I am encountering then comes when I try to reference this variable, data(loopcount).rawdata, later in the parfor loop. I receive an error when I try to reference it either by simply calling the variable name and having it print to the command window, or by trying to use it as in input to another function in the loop:
clear all; close all; clc;
N = 2;
parfor loopcount = 1:N
data(loopcount).rawdata = IOfunc(loopcount,inputinfo(loopcount))
data(loopcount).rawdata
output(loopcount) = nextfunc(data(loopcount).rawdata);
end
(both lines after the I0func will result in an error). The error I receive is: "Reference to a cleared variable data. Caused by: Reference to a cleared variable data." Do I need to initialize the "data" variable? I try including
data = struct;
before the parfor loop, but this results in different errors:
with N = 2 I get the error "Index exceed matrix dimensions. Caused by: Index exceeds matrix dimensions." and with N = 1 I get the error: "Subscripted assignment between dissimilar structures. Caused by: Subscripted assignment between dissimilar structures." When I change the parfor loop to a simple for loop, there are no errors. N = 2 is merely for demonstration and is likely much larger (motivating the use of the parfor loop rather than a simple for loop).
Please advise, thanks!

Best Answer

Unfortunately, in this case parfor has not correctly understood the way you are using a struct. To work around this problem, I would suggest using a cell array for data, like so:
parfor loopcount = 1:N
data{loopcount} = IOfunc(...);
...
end
You can convert the cell array back to the struct if necessary after the loop finishes like so:
data = struct('rawdata', data);