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

17 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2022-9-12
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 个评论
Pierre McGrail
Pierre McGrail 2023-8-22
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by