How to get 'Live Editor Evaluation Helper' .m file name from live script through another live script?
Show older comments
I am running two live scripts, let's call them main.mlx and sub.mlx. Below are minimal working examples.
The main.mlx:
dbstack().file
sub
The sub.mlx:
dbstack().file
When running dbstack().file in a live script, a file name "LiveEditorEvaluationHelper(randomNumbers).m" is returned. If sub.mlx is run manually, it returns a different Live Editor file name than when main.mlx is run (which it should). When sub.mlx is called through main.mlx though, the same Live Editor file name is returned twice, which is obviously incorrect.
I need to be able to get the "LiveEditorEvaluationHelper(randomNumbers).m" from sub through main. I have multiple subscripts, and the "LiveEditorEvaluationHelper(randomNumbers).m" changes everytime MATLAB is restarted, so running it once and then just saving the name as a variable is very impractical. Using
f = matlab.desktop.editor.getActiveFilename
also doesn't work, since the "active file" is main.mlx. I am assuming just calling sub.mlx through main.mlx doesn't activate the script, so it isn't added on the dbstack struct, but I can't seem to find a way around this. I have seemingly looked everywhere, and can't find an answer to this. I apologize in advance if the solution to this is glaringly obvious. Thanks for your help!
2 Comments
Steven Lord
on 24 Feb 2025
What specifically are you hoping/trying to do with this information if you can get it?
Answers (1)
Walter Roberson
on 24 Feb 2025
Edited: Walter Roberson
on 24 Feb 2025
When sub.mlx is called through main.mlx though, the same Live Editor file name is returned twice, which is obviously incorrect.
No, what you are seeing is the automatic output of ans because you do not have a semi-colon on the call to sub
file q2174453.mlx
dbstack().file
sub
file sub.mlx
dbstack().file
Output is
ans = 'LiveEditorEvaluationHelperEeditor78579DB3_B29EA684.m'
ans = 'sub.mlx'
ans = 'LiveEditorEvaluationHelperEeditor78579DB3_B29EA684.m'
I admit to being a bit unclear as to why ans at the end is not sub.mlx since sub.mlx is being executed as-if it were a script
Anyhow, to get at the LiveEditorEvaluationHelper(randomNumbers).m file name, use dbstack(1).file
5 Comments
Mathias
on 24 Feb 2025
Walter Roberson
on 24 Feb 2025
It turns out that the numeric parameter to dbstack is the number of frames to omit, so to get at the second frame only you need to assign the output of dbstack to a variable and then index the variable. (In the case where you know for sure that there are exactly two levels you can dbstack(1).file directly)
Walter Roberson
on 24 Feb 2025
file q2174453.mlx
'main is here'
dbstack().file
sub
file sub.mlx
'sub is here'
foo = dbstack;
foo(2).file
dbstack(1).file
Output is
ans = 'main is here'
ans = 'LiveEditorEvaluationHelperEeditor85515B63_925A5E4C.m'
ans = 'sub is here'
ans = 'LiveEditorEvaluationHelperEeditor85515B63_925A5E4C.m'
ans = 'LiveEditorEvaluationHelperEeditor85515B63_925A5E4C.m'
Walter Roberson
on 26 Feb 2025
In the case where a mlx is called from inside a mlx, as-if a function or script call, then it is not clear that LiveEditorEvaluationHelperEeditor....m is used. dbstack reflects the .mlx name in this case, whereas it reflects LiveEditorEvaluationHelperEeditor....m name for the outer mlx, so it might be that somehow the mlx is considered to be running directly.
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!