Determine own location in a live script

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 comentarios

Walter Roberson
Walter Roberson el 25 de En. de 2019
Tricky. It appears that evaluation of the live script is by copying it to a temporary directory and doing execution there. That appears to be done because it is common to evaluate section by section.
If you use dbstack('-completenames') then the file field of the first array element of the struct output refers to a file which is the mlx file, which is a zipped file containing a directory of xml files. But that doesn't tell you anything about where you are.
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Reno Filla
Reno Filla el 1 de Oct. de 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().

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 25 de En. de 2019
Editada: Rik el 3 de Mzo. de 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 comentarios

Stephen Bruder
Stephen Bruder el 25 de En. de 2019
I am NOT interested in where the current working directory is set to (i.e., current folder -> pwd), instead I would like to determine the addressof the folder containing the live script which I just opened (i.e., own location).
When opening a *.m file for the first time MATLAB prompts the user and asks "To run this file, you can either change the MATLAB current folder or add its folder to the MATLAB path," then, by selecting "change folder" the current folder gets set to the address/path of *.m file which was just opened.
How do I perform this task in a live script??
Rik
Rik el 25 de En. de 2019
You can still use the right click on the editor tab.
I also found this stackoverflow, which suggests the which strategy as well. More information is hard to find with Google, and my experience with the live editor is very limited, so if this doesn't solve it, I don't have a solution for you.
Adam Wyatt
Adam Wyatt el 10 de Sept. de 2020
I presume the OP needs to do this programatically and not via the GUI - I have the same problem!
I did partly resolve my problem by using Matlab projects. I could then obtain an object to the current project, and from that obtain the folder location of the project. This was sufficient for me since I wanted to reference a folder relative to that location and not necessarily the live script location (I essentially had a folder link to a data repository).
However, there are times where I want the folder location of the live script - so solutions welcome!
David Veit
David Veit el 7 de Dic. de 2020
I wasn't able to find a solution for this too. This would be a very important thing for me too as I share live scripts with people who are not very familiar with Matlab and they often forget to change the working directory, resulting in complaints that the script is not working because it's referencing files in the same folder.
goc3
goc3 el 21 de En. de 2021
This is one of the main reasons that I have generally not used live scripts...
I really hope that this will have a solution in the near future.
Qi Wang
Qi Wang el 1 de Mzo. de 2021
Editada: Qi Wang el 3 de Mzo. de 2021
Following trick works for me (R2020b):
fileparts(matlab.desktop.editor.getActiveFilename)
Rik
Rik el 3 de Mzo. de 2021
I added it to my answer to prevent it from disappearing into the older comments.

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Preguntada:

el 25 de En. de 2019

Comentada:

el 1 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by