Under any two years recent version of MATLAB, I am looking for the path of the current script being executed (ref.1, 2, 3, 4, 5),
Let's say, for a script through any of the following user procedures, in general, when a user runs a script:
- The "Run", "Run and Advance", "Run Section" or "Run and Time" menu buttons,
- The "Evaluate Selection" or "Evaluate Current Selection" contextual menus from the Editor,
- The F9 or Ctrl+Enter keyboard shortcuts from the Editor.
User procedures that could not be accepted by the solution, because there would be no associated script, are:
- Executing a piece of code directly in the command line,
- The contextual or keyboard shortcuts for executing a code from a Matlab interface or windows outside the Editor -Document, Browser, Help.
User procedures out of the scope of the question related to functions, which could be solved easily through any of the following indicated solutions, is:
- Calling the script as a function from the command line, from another script or from a function.
This is not for a function, which is trivially done through any of the following alternatives:
mfilename
mfilename('fullpath')
which(mfilename)
which('mfilename')
S = dbstack('-completenames');S(1).file
[folder,name,ext] = fileparts(which('object'))
And this is of course not the same that requesting the current folder through:
cd
pwd