Live script change context to current directory (same behavior as normal m files)
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pierre McGrail
el 12 de Sept. de 2022
Comentada: Pierre McGrail
el 22 de Ag. de 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 comentarios
Respuesta aceptada
Walter Roberson
el 12 de Sept. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!