Determine own location in a live script

53 views (last 30 days)
Stephen Bruder
Stephen Bruder on 25 Jan 2019
Commented: Reno Filla on 1 Oct 2021
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
Qi Wang on 3 Mar 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Reno Filla
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().

Sign in to comment.

Answers (1)

Rik
Rik on 25 Jan 2019
Edited: Rik on 3 Mar 2021
I see no indication that pwd doesn't work in live-scripts.
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:
There is also another thing you could try, @Qi Wang suggested using this:
fileparts(matlab.desktop.editor.getActiveFilename)
  7 Comments
Qi Wang
Qi Wang on 1 Mar 2021
Edited: Qi Wang on 3 Mar 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Rik
Rik on 3 Mar 2021
I added it to my answer to prevent it from disappearing into the older comments.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!