KLJ
KLJ

Reputation: 151

Find location of current script (mlx-file) in MATLAB

I'm working on my MATLAB code in a number of different locations, and it would be really helpful if I could make the code aware of its location on the computer. Till now I worked with .m-files. For .m-files I found the following solutions:

%example 1    
cd(fileparts(mfilename('fullpath')))

or

%example 2
tmp = matlab.desktop.editor.getActive;
cd(fileparts(tmp.Filename));

or

%example 3
S = dbstack('-completenames');
S(1).file

or

%example 4
which(mfilename)

But with MATLAB 2016a there comes a new feature called live script. And with that those solutions are not working anymore.

%For example I would like to do something like this
cd(MLX_FILELOCATION);
%or
which(mlxfilename)

(Edit III: Problem: I am not able to get the path/filelocation or name of the current opened/executed MATLAB-file. With *.m-files this is possible with the examples above. With *.mlx-files it is not possible anymore. And I prefere to use *.mlx-files instead of *.m-files.)

Outputs of the examples above executed in a *.mlx-file:

%example1: mfilename returns the path to the 'MatlabEvaluationHelper' in the 'AppData\Local\Temp'-folder
%example2: output is an empty array
%example3: same output as example1
%example4: same output as example1, because mfilename returns "MatlabEvaluationHelper"

Edit I: My first goal is that I would like to change the "current folder" (-> "cd") to the path of the running script. Why: In the same folder with the mlx-file I have for example .csv-files with data. And for example by tomorrow I have new folder. I copy the mlx_file and now I want to make sure that I don't use the csv-files from yesterday (because the current folder from yesterday is shown in the file browser of MATLAB) -> so I would like to change the "current folder" automatically with just copying the mlx-file into a new folder.

If there is a better practise for that, please let me know.

Thanks for helping

Edit II: Example for a used workflow: I programmed a MATLAB script. Saved it in folder "Dataset_ONE". Furthermure I copy "Dataset_ONE.csv"-file into the same folder. E.g. I now create a plot and save it as the "*.png" in folder "Dataset_ONE". The day after I might have a second (a new and with that different) dataset "Datasset_TWO". I create a new folder "Dataset_TWO". Copy the MATLAB-files to the new folder. Open the MATLAB file there. Then, because of the default settings, MATLAB has changed the "Current Folder" to the new folder where I opened MATLAB. But if I now open the MATLAB script in the first folder again (at the same time with the other dataset MATLAB script) I have to be careful about the current folder. In this case it might be useful to have the described solution.

Upvotes: 9

Views: 2796

Answers (2)

Brian61354270
Brian61354270

Reputation: 14423

As of at least MATLAB 2023b, your "example 2" now works in Live Scripts.

Running

>> temp = matlab.desktop.editor.getActive
>> f = temp.Filename

will retrieve the absolute path to the current Live Script into f. This works even when running only an individual cell in the Live Script --- no more complications with MatlabEvaluationHelper or LiveEditorEvaluationHelper*.

This can now also be shorted to just

>> f = matlab.desktop.editor.getActiveFilename

See

>> doc matlab.desktop.editor.getActiveFilename

for more detailed documentation.

Upvotes: 2

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

If what you want is some sort of way of preventing you running the wrong script on the wrong data without realising, then you could add a safety instruction at the top of each script, throwing an error if your current directory is not the same as the location of the script you're running. e.g.

>> assert (strcmp (pwd, '/absolute/path/to/my/script'));

As for loading the right data / saving to the right location, just load and save using absolute paths and there should be no confusion.

Upvotes: 1

Related Questions