How can we handle the conditional use of a parfor loop with the eval function?
Show older comments
Hi,
I'd like to use a parfor loop conditionally, and to use a simple for loop on the same code block otherwise.
The eval functions reports the following error message:
Error: At least one END is missing: the statement may begin here.
when executing the following code:
% start for/parfor loop
if bool_parfor
parpool(2);
eval('parfor k=1:length(vect_tmp)'); % > error message
else
eval('for k=1:length(vect_tmp)');
end
% code
...
% end for/parfor loop
if bool_parfor
eval('end')
p = gcp;
delete(p);
else
eval('end')
end
This topic has already been discussed in the following post, where the proposed solutions are to set the number of workers to 0 (it still slows down the process, so this is not an option), or to use two different blocks of code (or two functions), which is not efficient for development purposes.
As a consequence, I don't understand why even in the last versions of Maltab the eval function returns an error. When just entering the command 'for a=1:3' in the command window, Matlab is indeed expected to run until a 'end' command is entered by the user. Is there a way to avoid this issue in the eval function? Is it possible to access/modify its source code?
Thanks for your view,
Thomas
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!