Determine own location in a live script
27 views (last 30 days)
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
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)
Rik
on 25 Jan 2019
Edited: Rik
on 3 Mar 2021
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)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!