MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

pause(2)


disp ' Restoring the path'
rmpath(newpath);
end
end


Run the function as shown here. You can verify that the path has been restored by
comparing the length of the path before and after running the function:


origLen = length(path);


showImageOutsidePath('greens.jpg')
Opening the figure greens.jpg
Closing the figure
Restoring the path


currLen = length(path);
currLen == origLen
ans =
1


Retrieving Information About the Cleanup Routine


In Example 3 shown above, the cleanup routine and data needed to call it are contained in
a handle to an anonymous function:


@()restore_env(fig1, imgpath)


The details of that handle are then contained within the object returned by the
onCleanup function:


cleanupObj = onCleanup(@()restore_env(fig1, imgpath));


You can access these details using the task property of the cleanup object as shown here.
(Modify the showImageOutsidePath function by adding the following code just before
the comment line that says, “% This is the cleanup routine.”)


disp ' Displaying information from the function handle:'
task = cleanupObj.task;
fun = functions(task)
wsp = fun.workspace{2,1}
fprintf('\n');
pause(2);


Clean Up When Functions Complete
Free download pdf