Date: Apr 12, 2012 5:39 PM
Author: spasmous
Subject: Early exit from parfor loop?

I am looping over N files in a filesystem looking for M files with a
certain property. Sometimes they are found early and I can exit the
loop, however when using parfor there is no easy way to do this.

E.g. consider this code:

mydir = dir;
N = numel(dir);
myfiles = {}; % store filenames that pass <test>

parfor j = 1:N

if <test>
myfiles{j} = mydir(j);


The result is a cell array with some non-empty elements. This searches
all N files but if I want it to stop when it finds M=2 files, there
doesn't seem to be a way. This is probably well known behavior, but
I'm posting just in case anyone has a workaround... :)