Live script change context to current directory (same behavior as normal m files)

10 Ansichten (letzte 30 Tage)
Hi, is there any way to configure MATLAB so live scripts change to current directory when they are executed ? I am looking for the same behavior as .m file which ask to change the directory when they are executed.
Basically, the reason to change context is to be able to load some data files which are in the same dir as the mlx script. I want to use a relative path and don't want to use an absolute path.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Sep. 2022
No, no way is provided to configure Live Scripts to change directories (or ask to change directories) when invoked while the current directory is not the place the Live Script is stored.
When you execute a Live Script from the command prompt or the editor window, the first thing it does is create a new temporary file containing only the MATLAB code, omitting any controls, text, images and so on that you annotated the code with. It then executes the temporary file.
Because of this, it is normal for Live Script files to not really be executed from where they are stored... and Mathworks did not think to put in such a test themselves.
You will therefore have to use the kind of code tricks that you pointed out through that link.
  1 Kommentar
Pierre McGrail
Pierre McGrail am 22 Aug. 2023
thank you, I also recently found out that you can just right-click on the file tab and the first option in the context menu does basically that, changing the current folder to the one containing the mlx file

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by