Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 254
KLJ Avatar asked Aug 06 '16 09:08

KLJ


People also ask

How do I get the current file path in MATLAB?

To open the Current Directory browser, select Desktop -> Current Directory from the MATLAB desktop, or type filebrowser at the Command Window prompt.

Where are files stored in MATLAB?

This folder is a convenient place for storing files that you use with MATLAB. The default userpath folder is platform-specific. Windows® platforms — %USERPROFILE%/Documents/MATLAB . Mac platforms — $home/Documents/MATLAB .

How do I open an MLX file in MATLAB?

MATLAB converts publishing markup from the original script to formatted content in the new live script. To open an existing script ( . m ) as a live script ( . mlx ) from the Editor, right-click the document tab, and select Open scriptName as Live Script from the context menu.

How do I search for a file in MATLAB?

To open the Find Files tool, on the Home tab, in the File section, click Find Files. Enter your search criteria in the dialog box that opens. Use the Look in menu to specify the folders you want to search. Select Entire MATLAB Path to search all folders on the MATLAB search path.


1 Answers

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.

like image 51
Tasos Papastylianou Avatar answered Sep 23 '22 23:09

Tasos Papastylianou