I use a try-catch block in such cases. Example:
rng('shuffle')
try parpool('local'); end % try block to avoid reinitiation of parpool
parameters = normrnd(0,1,1000,1);
threshold=0.95;
FoundParameter=NaN;
try
parfor iParam=1:1000
result = sin(parameters(iParam))
if result < threshold
error(num2str(parameters(iParam)))
end
end
catch err
FoundParameter=str2num(err.message);
end
if isnan(FoundParameter)
fprintf('\nGood parameter was not found\n')
else
fprintf('\nGood parameter is: %f\n',FoundParameter)
end
PS Do not use i and j as iteration variables. This is bad practice at MatLab.
source
share