Determine own location in a live script

21 Ansichten (letzte 30 Tage)
Stephen Bruder
Stephen Bruder am 25 Jan. 2019
Kommentiert: Reno Filla am 1 Okt. 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 Kommentare
Qi Wang
Qi Wang am 3 Mär. 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Reno Filla
Reno Filla am 1 Okt. 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().

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 25 Jan. 2019
Bearbeitet: Rik am 3 Mär. 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 Kommentare
Qi Wang
Qi Wang am 1 Mär. 2021
Bearbeitet: Qi Wang am 3 Mär. 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Rik
Rik am 3 Mär. 2021
I added it to my answer to prevent it from disappearing into the older comments.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Scripts finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by