2

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
Community
  • 1
  • 1
Brethlosze
  • 1,533
  • 1
  • 22
  • 41
  • 1
    Can you be more precise on what's wrong with `mfilename` ? It works for scripts too, at least with the "Run" and similar buttons on R2016a under Ubuntu. – Ratbert Jun 15 '16 at 08:55
  • I have Matlab R2014b + Windows 8. The mode is to select a piece of code and run it, and the result is ''. The script or pieces of scripts are being executed manually... That is the standard behavior of mfilename. – Brethlosze Jun 15 '16 at 08:57
  • 1
    I doubt you will ever find a solution for the "Evaluate selection" mode, because it is like a copy and paste of the code into the command window. The code is then totally unrelated to any file. – Ratbert Jun 15 '16 at 09:02
  • That is right, but i am not 100% sure. Perhaps there is an indirect method, able to request the executing scope of the interface. I dont discard a java procedure being able to provide that information natively from matlab. – Brethlosze Jun 15 '16 at 09:05
  • 1
    When you execute code using the methods you mention, MATLAB is pretty much literally just copying the code and `eval`ing it within the base workspace. As @Ratbert says, it's no longer related to a particular `.m` file so `mfilename` can't be expected to work, and there is no useful scope that you're going to get from `dbstack`. I think matlabgui's answer is as close as you're going to get, but even that will fail pretty easily. – Sam Roberts Jun 15 '16 at 15:54
  • @SamRoberts. Hehehhehe... As the statement of the question, the matlabgui answer is ok by now. Much better that the "it is impossible but i am not totally sure" feeling doubt. – Brethlosze Jun 15 '16 at 17:19

1 Answers1

4

edit I misunderstood the question initially...

So with hopefully a bit better understanding you could try and utilise:

matlab.desktop.editor.getActiveFilename

to get the current active file in the editor -> which should give the current script...

Note: this is an undocumented function which may change between releases -> it gives me the active filename in r2015b.

matlabgui
  • 5,642
  • 1
  • 12
  • 15
  • 2
    This is unrelated to what the OP is asking: the source script can be anywhere in the path, there is no reason for it to be in the current folder. – Ratbert Jun 15 '16 at 09:48
  • 1
    @Ratbert - indeed I thought my 1st answer was to simple... and so must have been missing something – matlabgui Jun 15 '16 at 10:14
  • 1
    Good catch ! But this fails for scripts called by another script, or if the script is called directly from the command line (it may perfectly well not be opened in the editor ...) – Ratbert Jun 15 '16 at 10:18
  • 1
    True - but in that case `mfilename` should work... So possibly the full solution might be a combination of the two? – matlabgui Jun 15 '16 at 10:22
  • 1
    Indeed. You would call `mfilename`, and if empty get the value of `matlab.desktop.editor.getActiveFilename`. The only caveat I see (for the moment) is that it can give you a false positive when the code is executed directly from the command window. The normal behavior would then be an empty result while this method will return the active file in the editor, which can be anything and totally unrelated to the executed code. – Ratbert Jun 15 '16 at 10:28
  • 1
    True - i agree the risk of false positives are high - but as you said in your comment `evaluating section` is just a copy paste to the terminal -> so 100% accurate solutions to all scenarios may be unreachable. – matlabgui Jun 15 '16 at 10:33
  • 1
    One problem is that it will fail if the user simply switches tabs in the editor while the code is running. – Sam Roberts Jun 15 '16 at 15:49
  • 1
    @SamRoberts True - Due to the issues mentioned here and in the comments I dont think a 100% fullproof answer will be possible – matlabgui Jun 15 '16 at 16:28
  • And works under MATLAB R1024b too. This is a great answer, which will be remembered through the eons. This works fine when called under all the indicated scripting executing procedures. And of course, when there is no filename, nothing but 'Untitled' is returned, which is the proper answer when there is actually no script. – Brethlosze Jun 15 '16 at 17:06
  • @Ratbert When a script is being called as function, mfilename works. That is a solved case. Note that execution method is out of the scope of the question. And also, when the script is invoked from the command line, you are running it as a function. Also, if you copypaste the script in the command line, you are not running the script anymore, but its copy. – Brethlosze Jun 15 '16 at 17:10
  • @SamRoberts Yes, it will fail under that case. If a better solution is found to exist, perhaps somebody will include it here. – Brethlosze Jun 15 '16 at 17:13
  • cwd=mfilename; if isempty(cwd) cwd=matlab.desktop.editor.getActiveFilename disp('Active Filename'); else cwd disp('Normal Filename'); end – Brethlosze Jun 15 '16 at 17:33