How can I get script’s location when running one section of a script?
I frequently run scripts one section at a time. I can be using different computers, remote desktops, etc., so the path changes. The load and save commands are relative to the present working directory rather than the directory of the script, which causes problems if the present working directory isn’t where the script is. When running a whole script, I can use either mfilename(‘fullpath’) or dbstack(‘-completenames’) to get the directory the script is in, but this doesn’t work when running a section. How can I achieve this? None of the following work:
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
stk = dbstack(‘-completenames’);
filepath = stk(1).file
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
mfilename(‘fullpath’)
% Returns wrong filename, because I’ve moved to another script by the time
% it gets to this line.
filePath = matlab.desktop.editor.getActiveFilename
If it’s impossible with MATLAB code, perhaps there is a Java-based workaround?I frequently run scripts one section at a time. I can be using different computers, remote desktops, etc., so the path changes. The load and save commands are relative to the present working directory rather than the directory of the script, which causes problems if the present working directory isn’t where the script is. When running a whole script, I can use either mfilename(‘fullpath’) or dbstack(‘-completenames’) to get the directory the script is in, but this doesn’t work when running a section. How can I achieve this? None of the following work:
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
stk = dbstack(‘-completenames’);
filepath = stk(1).file
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
mfilename(‘fullpath’)
% Returns wrong filename, because I’ve moved to another script by the time
% it gets to this line.
filePath = matlab.desktop.editor.getActiveFilename
If it’s impossible with MATLAB code, perhaps there is a Java-based workaround? I frequently run scripts one section at a time. I can be using different computers, remote desktops, etc., so the path changes. The load and save commands are relative to the present working directory rather than the directory of the script, which causes problems if the present working directory isn’t where the script is. When running a whole script, I can use either mfilename(‘fullpath’) or dbstack(‘-completenames’) to get the directory the script is in, but this doesn’t work when running a section. How can I achieve this? None of the following work:
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
stk = dbstack(‘-completenames’);
filepath = stk(1).file
% Returns ‘C:UsersMyNameAppDataLocalTempEditor_mddonLiveEditorEvaluationHelperE847216552.m’
mfilename(‘fullpath’)
% Returns wrong filename, because I’ve moved to another script by the time
% it gets to this line.
filePath = matlab.desktop.editor.getActiveFilename
If it’s impossible with MATLAB code, perhaps there is a Java-based workaround? pwd, directory, section, current directory MATLAB Answers — New Questions