Determine own location in a live script
Show older comments
Using live script: How do I make the live script aware of (or determine) its own file location? This is a simple task with a *.m file script. This causes problems when using a live script to call/run other scripts/simulink-files located in the same folder (as the live script) as the "current folder" is not updated to the live script's location (as is the case in a *.m file).
3 Comments
Walter Roberson
on 25 Jan 2019
Tricky. It appears that evaluation of the live script is by copying it to a temporary directory and doing execution there. That appears to be done because it is common to evaluate section by section.
If you use dbstack('-completenames') then the file field of the first array element of the struct output refers to a file which is the mlx file, which is a zipped file containing a directory of xml files. But that doesn't tell you anything about where you are.
Qi Wang
on 3 Mar 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Reno Filla
on 1 Oct 2021
Thanks Qi Wang, this is a live saver! I was getting crazy that Live Script would only get me "LiveEditorEvaluationHelpExxxxx" as reply to mfilename().
Answers (1)
If you can't guarantee the current folder, but do know the file name, you can use which.
dbstack doesn't seem to work as expected in my tests.
Edit:
fileparts(matlab.desktop.editor.getActiveFilename)
7 Comments
Stephen Bruder
on 25 Jan 2019
Rik
on 25 Jan 2019
You can still use the right click on the editor tab.
I also found this stackoverflow, which suggests the which strategy as well. More information is hard to find with Google, and my experience with the live editor is very limited, so if this doesn't solve it, I don't have a solution for you.
Adam Wyatt
on 10 Sep 2020
I presume the OP needs to do this programatically and not via the GUI - I have the same problem!
I did partly resolve my problem by using Matlab projects. I could then obtain an object to the current project, and from that obtain the folder location of the project. This was sufficient for me since I wanted to reference a folder relative to that location and not necessarily the live script location (I essentially had a folder link to a data repository).
However, there are times where I want the folder location of the live script - so solutions welcome!
David Veit
on 7 Dec 2020
I wasn't able to find a solution for this too. This would be a very important thing for me too as I share live scripts with people who are not very familiar with Matlab and they often forget to change the working directory, resulting in complaints that the script is not working because it's referencing files in the same folder.
goc3
on 21 Jan 2021
This is one of the main reasons that I have generally not used live scripts...
I really hope that this will have a solution in the near future.
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Rik
on 3 Mar 2021
I added it to my answer to prevent it from disappearing into the older comments.
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!