Changing back to original folder after an error occured

10 views (last 30 days)
Hello,
I created a function which requires several user inputs. Some input combinations can cause an error. During the execution of the function, some directory changes happen. After the function has finished, it returns in the original folder but in case an error occurs, it will be stuck in of the other directories. How can I automatically switch back to the Folder where the .m file is located?
I read about the try and catch function but I'm unsure if they work for what I want and how I would use them. If they are suitable for my problem, can you give an example in pseudocode how to use it?
Thank you.
  2 Comments
Stephen23
Stephen23 on 24 Feb 2016
Edited: Stephen23 on 24 Feb 2016
The best solution is do not change directories. There is rarely a reason for doing this. If you only need to access files in those other folders then just create absolute or relative paths and use these. This is much faster and much more robust.
Summary: Switching directories is slow, a pain to debug, and a pain when it gets messed up. It is much simpler and faster to use absolute or relative filepaths. All MATLAB functions accept them, so there is no reason why you should not use them.
Jan
Jan on 24 Feb 2016
@Stephen: Please post this as an answer, because it is the best solution. The current folder might be changed unexpectedly in a timer or GUI callback during a function is running. So using absolute paths in every case is the only reliable method.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 24 Feb 2016
The best solution is do not change directories. There is rarely a reason for doing this. If you only need to access files in those other folders then just create absolute or relative paths and use these. This is much faster and much more robust.
Summary: Switching directories is slow, a pain to debug, and a pain when it gets messed up. It is much simpler and faster to use absolute or relative filepaths. All MATLAB functions accept them, so there is no reason why you should not use them.

More Answers (1)

Steven Lord
Steven Lord on 24 Feb 2016
You can create an onCleanup object that will CD back to the original directory when it leaves scope. The whole purpose of the onCleanup object is to die and with its last breath do something.
function onCleanupExample
P = pwd;
goback = onCleanup(@() cd(P));
fprintf('Before CD, inside %s\n', P)
cd(tempdir)
fprintf('After CD, inside %s\n', pwd)
error('Throw an error');
Assuming you weren't in your TEMPDIR before, you should see that you are back in the directory printed in the "Before CD" line. You should also see the error message.
[Just as an aside, If you were doing this in the context of a test class in the unit testing framework, I would recommend using the addTeardown method instead.]

Categories

Find more on Entering Commands in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!