Clear Filters
Clear Filters

How to know if current codes are running from Live Script file

3 views (last 30 days)
Is there a function in MATLAB to show if the program code or a function is running from within a Live Script .mlx file or from within a plain .m file?
If the code runs from within an .mlx file , I want it to behave or act differently.

Answers (1)

Riya on 1 Feb 2024
In MATLAB, there is not any built-in function that directly checks if the code is running from a Live Script (.mlx) or a regular script (.m). However, you can use a combination of functions to infer the environment from which the script is running.
One possible approach is to use the `dbstack` function to get the call stack and then analyze the file extensions of the calling functions. Here is a sample function that attempts to deduce whether the current function is being called from within a Live Script:
function isLiveScript = runningFromLiveScript()
% Get the stack information
stackInfo = dbstack('-completenames');
% Initialize the flag
isLiveScript = false;
% Check if the stack is not empty
if ~isempty(stackInfo)
% Loop through each item in the call stack
for i = 1:length(stackInfo)
[~, ~, ext] = fileparts(stackInfo(i).file);
% Check if the file extension is '.mlx'
if strcmp(ext, '.mlx')
isLiveScript = true;
You can use this function in your code to determine the behavior based on whether it's running from a Live Script or not. Here's how you might use it:
if runningFromLiveScript()
disp('This code is running from a Live Script.');
% Put your Live Script specific code here
disp('This code is running from a regular .m script.');
% Put your regular script specific code here
It is based on the assumption that the call stack will include the .mlx file if it is being run from a Live Script. If the Live Script calls a function that, in turn, calls your code, this method should work. However, it may not work in all scenarios, especially if the Live Script is running a script that calls a function without creating a stack entry for the Live Script itself.
For more information refer following documentation:


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!