%% Use recreate to Resubmit Tasks with Errors
% This approach is useful when tasks depend on a file that is not present anymore.
% Create a new job using the default cluster profile. In this example, it is the local parallel pool.

cluster = parcluster;
job = createJob(cluster);

% Create several tasks. In particular, create a task that depends on a MAT-file that does not exist.

createTask(job,@() 'Task1',1);
createTask(job,@() load('myData.mat'),1);

% Submit the job, and wait for it to finish. Because the MAT-file in the second task does not exist, the job fails. If you call fetchOutputs on job to retrieve the results, you get an error. Check the error using the Error property of the corresponding task.

submit(job);
wait(job);
job.Tasks(2).Error

% Create the MAT-file referenced from the second task using the save function. To create a new job with the tasks that resulted in an error, use the 'Tasks' name-value pair in recreate, and provide the hasError function. If you want to select a different set of tasks, you can define your own function.

str = 'Task2';
save myData str
newjob = recreate(job,'Tasks',@hasError);

% Submit the new job, wait for its completion, and fetch the outputs. Because the MAT-file now exists, the job does not fail.

submit(newjob);
wait(newjob);
out = fetchOutputs(newjob);
out{1}

% Copyright 2018 The MathWorks, Inc.