Live script change context to current directory (same behavior as normal m files)
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pierre McGrail
am 12 Sep. 2022
Kommentiert: Pierre McGrail
am 22 Aug. 2023
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.
Right now, I have to add some code to do that (see https://www.mathworks.com/matlabcentral/answers/416718-automatically-add-an-opened-live-script-s-directory-to-the-search-path-for-the-current-session)
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.
0 Kommentare
Akzeptierte Antwort
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!