When I open MATLAB, it defaults to open to /home/myUser. Whenever I open a file interactively (say I run Simulink and click “Open…”) the dialog will start in /home/myUser. Then I might go into /home/myUser/myDir1/myDir2/ before clicking on myModel.mdl.
If I open a different Simulink file with the “Open…” dialog again, it’ll kick me back to /home/myUser. Note that this is regardless of file, I’m just using Simulink as an example. I would like to keep it in /home/myUser/myDir1/myDir2, meaning I should be in the same directory as the last file I just opened (or saved).
Programatically, I would set up a callback to cd into whatever directory the file I chose was in after selecting the file using uigetfile. Is this possible to do just with MATLAB’s own “Open” or “Save As” commands?
There is a wrapper for
uigetfilethat remembers the last director on the file exchange. You can get wrappers for the other file dialogs as well from the same author.EDIT
How to overload the built-in
uigetdiretc.(1) Rename
uigetdir2touigetdir, and make sure it’s in a path that has precedence over the path for the built-in function (this should be the case by default).(2) Use BUILTIN to ensure that the new function doesn’t call itself.(2) Since
uigetdiris implemented as a .m-file, as opposed to being compiled (likesum), thebuiltincommand doesn’t work for it. Thus, openuigetdir, find the private functionuigetdir_helper(which is private, so we can’t call it), and finally uncover the java method (that turns out to have changed from R2011a to R2011b. Yay.). This allows us to finally overloaduigetdirat the cost of having to parse the input ourselves.Here’s lines 37 to 67 from the modified
uigetdir