How can I get script's location when running one section of a script?

58 vues (au cours des 30 derniers jours)
Leon
Leon le 13 Juin 2024
Modifié(e) : Leon le 14 Juin 2024
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:\Users\MyName\AppData\Local\Temp\Editor_mddon\LiveEditorEvaluationHelperE847216552.m'
stk = dbstack('-completenames');
filepath = stk(1).file
% Returns 'C:\Users\MyName\AppData\Local\Temp\Editor_mddon\LiveEditorEvaluationHelperE847216552.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?

Réponses (2)

Leon
Leon le 14 Juin 2024
Modifié(e) : Leon le 14 Juin 2024
I seem to have found a workaround by creating a function in a separate .m in the same folder that calls mfilename("fullpath"), and Matlab is smart enough to give the directory of that function rather than the live editor temporary file location.
Separate .m file get_current_dir.m :
function current_dir = get_current_dir()
current_dir = fileparts(mfilename("fullpath"));
end
Then if I do:
a = mfilename('fullpath')
b = get_current_dir()
a = 'C:\Users\MyName\AppData\Local\Temp\Editor_mddon\LiveEditorEvaluationHelperE847216552'
b =
'C:\MatlabFolder'
It's a shame to need a separate .m file to do something fundamental that should be built-in, and I try to trail around the feasible minimum number of files, but it's certainly doable if this is a robust solution. Any pitfalls of this method I should be aware of? Thanks.

Steven Lord
Steven Lord le 13 Juin 2024
Rather than depending on what is the current directory when running the code (which is subject to change when you're running the code by sections, as you've already noticed) why not store the data in a fixed location that you can compute using functions included in MATLAB? On the same computer I believe tempdir will be the same across MATLAB sessions unless you explicitly change one of a few environment variables. If you need a temporary file name, you can use tempname or use fullfile to append a name of your choosing to tempdir.
If you have multiple machines tempdir can return different directory names (a directory name that works on Linux won't work on Windows and vice versa, for example) but it will be a valid temporary directory name for the machine on which you're working.
td = tempdir
td = '/tmp/'
tn1 = tempname
tn1 = '/tmp/tpd092c5d1_a757_443c_9a11_83e10e88e6e5'
tn2 = tempname('c:\Temp') % Yes, writing here almost certainly won't make sense on Linux
tn2 = 'c:\Temp/tpae7e5076_36d1_47dc_99cb_62bfe13b8d7d'
  1 commentaire
Leon
Leon le 14 Juin 2024
Modifié(e) : Leon le 14 Juin 2024
Perhaps I don't understand you, but I'm not trying to save something temporarily. Let's say I have two files, my_script.m and my_data.m in a folder called Data. If I run a section of my script in my_script.m and do load("Data\my_data.m"), I get an error telling me that the folder "Data" doesn't exist. When I check the pwd it is somewhere else; the pwd isn't the current directory. I need to set the pwd to the same directory the script is in, or know what the directory of the script is to set the path when using load() and save().
Here is an example. When I run the following section:
%% Section nine
a=5;
save("Test\test.m","a")
Error using save
Cannot create 'test.m' because 'Test' does not exist.
Test does of course exist, I just need some way to tell Matlab to use the same directory the script is in, which really shouldn't be so hard...
I don't have admin permissions on some computers, so I don't think I could go creating C:\temp for example, and I want to have my data in the same folder as my script (in a subfolder). I also don't want to have data in temporary folders that may get deleted or written over my other programs.
Is there really no way to get the folder of where the script is?
Edit: I seem to have found a workaround; see my "answer".

Connectez-vous pour commenter.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by