Changing back to original folder after an error occured

5 Ansichten (letzte 30 Tage)
Daniel Sieben
Daniel Sieben am 24 Feb. 2016
Kommentiert: Daniel Sieben am 25 Feb. 2016
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 Kommentare
Stephen23
Stephen23 am 24 Feb. 2016
Bearbeitet: Stephen23 am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 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.

Weitere Antworten (1)

Steven Lord
Steven Lord am 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.]

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by