7

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.

KLJ
  • 131
  • 1
  • 10
  • KLJ it's not very clear to me what you're trying to do still. Sounds like you're just trying to add a bunch of folders to the matlab path to make them accessible at all times, which is very simple ... – Tasos Papastylianou Aug 06 '16 at 12:01
  • @tasos-papastylianou: See "Edit I" above. Thanks for asking. – KLJ Aug 06 '16 at 12:31
  • it really depends how you start matlab, what's in your startupscripts etc ... please write an example of what your desired workflow would be like, including how and where you launch matlab from, what your folder structure is like in general with respect to all relevant files involved, what the initial directory matlab lands you on is, where you'd like it to be instead, and what you'd like to have access to (and what you would *not* like it to have access to). – Tasos Papastylianou Aug 06 '16 at 12:40
  • @tasos-papastylianou: See "Edit II" above. Does it make an usecase more clear? – KLJ Aug 06 '16 at 14:58
  • 1
    I have the same problem - I need to load data from the same folder (or subfolder) of where the script is located, but if I run the script when I'm in a different folder, Matlab doesn't have any issues until it doesn't find the file. With scripts, Matlab automatically asks if you want to change folder. I would like the same, or automatically change the folder. I cannot use absolute paths because I run the script on different computers. – Wybird666 Jan 16 '19 at 21:16
  • I suspect the OP seeks to return the directory of the current .mlx livescript. – gatorback Sep 03 '19 at 12:18
  • 1
    FYI, example 2 works for me (Matlab 2020b) and returns the correct local file path where the .mlx script is located. – Jan Dec 10 '20 at 17:39

1 Answers1

1

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.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • Thanks for the answer. This is kind of good, but I am looking for a full automated generation of the absolute path to change the "Current Folder" corretly. Might there be an other solution? – KLJ Aug 06 '16 at 15:16
  • And with a fully automated generation of the absolute path one could run the same folder on a different PC without any problems. – KLJ Aug 06 '16 at 15:20
  • *if* the file is unique and on the matlab path, then you can get its full path with the `which` command. I do not have matlab 2016 so I cannot test mlx files, but presumably if they are runnable then they should also be detected by the `which` command. You can use this output to automate the check you want. Having said that, a lot of 'automation' requests are usually not that well thought out. You can only automate 'checks and procedures'. You can't magically guess undefined behaviour. It sounds to me you want something that will "just work" regardless how unpredictably you treat the file... – Tasos Papastylianou Aug 06 '16 at 23:09
  • You are right that in general one could get the path with 'which'. Sry that I forgot that kind of example. I just updated the examples above. Now my problem is that I am not able to get the filename of the current script (mfilename doesn't work in mlx-files). So I somehow need a function "mlxfilename". – KLJ Aug 07 '16 at 15:45
  • @KLJ sorry, I can't help more specifically as I don't have matlab 2016 to test solutions :/ – Tasos Papastylianou Aug 07 '16 at 21:03