Parfor loop variable cannot be classified!

When I try to execute the above code with parfor instead of using for, I get the following error :
Error: The variable study in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".
I think the "Form of Indexing : Within the list of indices for the variable, exactly one index involves the loop variable." rule for sliced variables in parfor is causing the issue. (Because study(i).run(j) has not been indexed according to parfor requirements.) But since I have a structure with different fields and I need to update the field 'run' in the structure 'study', is there any way i could make this code compatible with parfor ? Any help/suggestion is really appreciated!

 采纳的回答

No, a parallelization of this code is not possible. E.g. in this line:
study(i).run(j).result = ...
You write a specific value, which depends on the PARFOR loop counter j. Afterwards you try to save the struct study to a file:
save([study_path study(i).name '.mat'], 'study');
But then this file will be overwritten in each iteration and the contents of the struct depends on the currently processed thread. This is such confusing and the resulting file will contain the struct as created in the last processed thread - which is a kind of random.
At first I'd move the writing of the file outside the PARFOR loop, such that 1. the slow and colliding disk access does not slow down the program dramatically and 2. the final value of the file is well-defined and reproducible.
When the netsed struct is still a problem, try a cell (what is "i"?!):
runC = cell(1, size(study(i).J, 1));
...
Sorry, I give up here. The nested creation of nested structs looks too confusing. But the general idea would to create the different data separately in a cell array and insert them to the struct afterwards.

5 个评论

'i' is the length of 'study' .. I have added a bit more code chunk to clarify (i know now it looks more confusing anyways!) .. thanks for your suggestion ... i'll move 'save' out of the loop and save 'study' after loop ends .. also i'll try removing index i since it's always going to be 1 in my case!
I have created cell arrays and inserted the struct back afterwards as you suggested. Now I receive the following error :
Error using parallel_function (line 589)
Undefined function or variable 'exist_study'.
Error stack:
(No remote error stack)
Suggestions please !!
You explain, that you have changed the code substantially. Then we cannot find the reason of a bug, without seeing the code. Obviously the variable "exist_study" is not existing, when this line is called. Perhaps it has been cleared or not provided. Show us at least the failing line of code.
Sorry forgot to mention that I had updated the code in the original post itself(check the original question above). let me know if you are not able to see the changes. Thanks for your help!
ignore the question ! my bad, the code had a logic error .. exist_study doesnt exist when the study.name.mat isnt present .. i understood the mistake ! Thanks again for your time.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by