mfilename not working on Live Editor?
35 views (last 30 days)
Show older comments
Jaime López
on 20 Oct 2017
Commented: John Brown
on 11 Jun 2025 at 19:21
Hi. I'm using matlab 2016a. Inside a Live Editor script, I'm trying to get the script name, using the function mfilename, without any parameters, to get it. What matlab gives as an output is: MatlabEvaluationHelper.

I've tried to export this code to a classic matlab .m file, and seems to work fine. Any idea on what could be the problem? Thanks in advance
0 Comments
Accepted Answer
Cam Salzberger
on 20 Oct 2017
Hello Jaime,
I am afraid that mfilename does not function inside live scripts or functions. Our developers are aware of this feature request, and are considering possible resolutions for future releases of MATLAB.
Can you describe what you are trying to accomplish with "mfilename"? There may be other ways to get a similar workflow to what you desire.
-Cam
12 Comments
Ornit
on 21 Jul 2022
mfilename still does not work properly in live editor, R2022a. Returns some location on my C drive (while the script is in fact running on a network drive).
However, Jan Kappen idea (filePath = matlab.desktop.editor.getActiveFilename; from 13 Jan 2022) works perfectly. Thanks!
Bradley Stiritz
on 6 Oct 2022
Thank you Jan Kappen for the workaround! and thanks Omit for the +1
After 5 years, it would be nice if the mfilename doc page could be updated to mention the limitation and workaround.
More Answers (1)
John Brown
on 11 Jun 2025 at 19:17
General
mfilename() does not work because live scripts use a temporary "live editor evaluation helper file," so mfile is evaluated there. I too found myself wishing that mfilename() did work and so I went ahead and made myself a few helpers; among them is currentFile() which I'll go ahead and dropin the event you're.
Full warnings:
- dbstack() isn't great for performance and Mathworks documentation explicitly warns its best to be avoid programmatically because of this.
- Im using a simplistic string match to check for live script mode, this could break in the future.
- I have included the dependency dbstack_() and its internal functions; If you wrap up dbstack_() and the two function after it into a file then wrap currentFile() into another function file this should work just fine.
- In the event you want to debug this in any capacity, or are notice weird behavior, note that the method of gathering the current file in the event a live script is the caller, is checking the open file in the editor. This means if you put a breakpoint down in the function and step over the call that grabs the open file, you wont get the result you would in the standard case. This also applies if you were to run a live script file using this method and then change the open editor file before execution of the function. That said these seem unlikely and for most practical use cases this works wonderfully.
Usage
To use the function and return the filename, (which in the event the file is unsaved will return untitledN, I prefer this but you could easily modify that) you simple call:
filename = currentFile();
If you would prefer the absolute path of the file, you can call the function with "fullpath." In the event that the file is unsaved the path returned will be equivalent to fullfile(pwd(), "untitledN") ie the path youd see if you were to save it in the current workspace. Note that Ive not yet made the appropriate change to check user settings and append the .m vs .mlx so the returned filename of unsaved scripts will lack an extensions:
filename = currentFile("fullpath");
Function Definitions
currentFile() Function File Contents:
This function will return the file name of the caller. If the stackUpCount variable is specified the number of items to be removed from the stack can be specified. This function has be written to work with live script execution where mfilename() fails to provide the file name, this function determines if the caller is a live script and uses alternative functionality to return the path of the file that is being run as a live script.
Note: Because the function uses the matlab.desktop.editor.ActiveFilename() method, you must be in the caller file during execution of this script for it to work. This is normally not an issue unless you are stepping into this function; doing so makes this function the active file, thus producing incorrect output.
function [filePath, lineNumber] = currentFile(pathType, opts)
arguments
pathType (1, 1) string {mustBeMember(pathType, ["", "fullpath"])} = "";
opts.StackCount (1, 1) double {mustBeInteger(opts.StackCount)} = 0;
opts.IncludeExtension (1, 1) logical = true;
opts.OutputFormat (1, 1) string {mustBeMember(opts.OutputFormat, ["Uniform", "Struct", "Table"])} = "Uniform";
end
% Calls dbstack to get information about the caller
stack = dbstack_(pathType, "StackCount", opts.StackCount + 2, "IncludeExtension", opts.IncludeExtension);
stack = stack(1);
% Assigns the line number
lineNumber = stack.line;
% Handles conforming to desired output format
switch(opts.OutputFormat)
case "Uniform"
filePath = stack.file;
case "Struct"
filePath = stack;
case "Table"
filePath = struct2table(stack);
end
end
dbstack_() Function File Contents:
Similar to the built in dbstack but will replace instances of the live script helper file with the currently open editor file.
function stack = dbstack_(pathType, opts)
arguments
pathType (1, 1) string {mustBeMember(pathType, ["", "fullpath"])} = "";
opts.StackCount (1, 1) double {mustBeInteger(opts.StackCount)} = 1;
opts.IncludeExtension (1, 1) logical = true;
end
% Calls dbstack to get information about the caller
stack = dbstack(opts.StackCount, "-completenames");
% Gathers the stack info for live script substitution
eStack = struct2table(editorStack());
% Handles determination of output when caller is live script execution
if(isempty(stack))
stack = eStack;
else
% Normalize stack for modification
stack = convertCharsToStrings(struct2table(stack));
stack = convertvars(stack, ["file", "name"], "string");
% Changes the files referencing a live script helper
needsModified = referencesLivescript(stack.file);
stack.file(needsModified) = strings([sum(needsModified), 1]) + eStack.file;
% Changes the names referencing a live script helper
needsModified = referencesLivescript(stack.name);
stack.name(needsModified) = strings([sum(needsModified), 1]) + eStack.name;
end
% Separates the elements of the file path
[folder, name, ext] = fileparts(stack.file);
% Includes extension if desired
if(opts.IncludeExtension)
name = name + ext;
end
% Handles rebuilding file appropriately
if(pathType == "fullpath")
stack.file = fullfile(folder, name);
else
stack.file = name;
end
% Converts to struct to maintain consistency with dbstack()
stack = table2struct(stack);
end
Returns a structure array compatible with the return of dbstack() using the active live script instead.
function stack = editorStack()
% Gather the active file
stack.file = string(matlab.desktop.editor.getActiveFilename);
% Builds file path if file were saved in the current directory
if(~isfile(stack.file))
stack.file = fullfile(pwd(), stack.file);
end
% Gathers the name
[~, name] = fileparts(stack.file);
stack.name = name;
% Gathers currently selected line
stack.line = matlab.desktop.editor.getActive().Selection(1);
end
Determines whether the string refers to a live script evaluator.
function b = referencesLivescript(str)
arguments
str string;
end
% Determines if the caller is a live script being run in the editor
liveScriptStrCmp = "LiveEditorEvaluation";
b = str.contains(liveScriptStrCmp, "IgnoreCase", true);
end
1 Comment
John Brown
on 11 Jun 2025 at 19:21
For completions sake, here is the folder equivalent mirror of currentFile(), currentFolder(), that just strips the folder from the path as well:
currentFolder() Function File Contents:
Returns the folder of the currently running file. This function uses currentFile() so if you find weird behavior with this function the cause is likely from currentFile().
function folderPath = currentFolder(pathType, opts)
arguments
pathType (1, 1) string {mustBeMember(pathType, ["", "fullpath"])} = "";
opts.StackCount (1, 1) double {mustBeInteger, mustBeGreaterThanOrEqual(opts.StackCount, 0)} = 0;
end
% Gathers the full folder path of the current running file
[folderPath, ~] = fileparts(currentFile("fullpath", "StackCount", opts.StackCount + 1));
% Removes the absolute path from the folder path
if(pathType == "")
[~, folderPath] = fileparts(folderPath);
end
end
See Also
Categories
Find more on Debugging and Analysis 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!