Parfor - classification of variables in multiple nested loops

1 view (last 30 days)
My variable "tmp" cannot be classified. How can this be edited for it to work in parfor form?
w = [1:5];
x = [1:5];
y = [1:5];
z = [1:5];
parfor i=1:length(w);
for j=1:length(x);
for k=1:length(y);
count=0;
for l=z;
count=count+1;
file = ['filepath.' k '-' l '.nc'];
ncfile = netcdf.open(file);
t = netcdf.getVar(ncfile,1);
tmp(count,y+1) = t(w(i),x(j));
end
end
end
end
  1 Comment
Walter Roberson
Walter Roberson on 12 Aug 2015
Caution: ['filepath.' k '-' l '.nc'] is going to use the numeric value of k and l in the indicated positions, so the code is equivalent to
file = ['filepath.' char(k) '-' char(l) '.nc'];
with k and l each being in the range 1 to 5, that is going to be using the characters SOH, STX, ETX, EOT, ENQ in those positions. If you want '1' through '5' to be used instead of char(1) through char(5) then you need to convert the numbers to text. For example,
file = sprintf('filepath.%d-%d.nc', k, l);

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Aug 2015
Your parfor is over "i". Essentially your output needs to be indexed over "i". Not indexed over w(i), over i directly. Cell arrays and struct arrays are acceptable. For example,
data_w{i} = ....
would be acceptable but not
data_w{w(i)}
It would be allowed to use
parfor i = w
and index by i, but on output to data_w{i} the cells used would not be, in the general case be adjacent. With those particular w values they would be adjacent.
You effectively only get one shot at assigning to something indexed at the parfor loop variable, so all nested "for" should be building into a variable and then the last statement inside the parfor should be to copy that variable into the locations indexed at the loop variable. It is acceptable to copy entire columns or sub-matrices, such as
data_w(:,:,:,i) = data_built_in_the_loop;
provided of course that the data is always exactly the same size.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!