How does MATLAB know it ran from vscode or desktop?

11 views (last 30 days)
I am setting up a startup.m script.
I want to implement different behavier in this script depending on the starting mode of MATLAB.
For example, If MATLAB is ran from double clik on desktop, the startup.m executes code_1, while if MATLAB is started by vscode official extension, the startup.m executes code_2.
Are there anyone know how to distinguish the two different starting mode by MATLAB itself?

Accepted Answer

lala
lala on 13 Dec 2023
Edited: lala on 13 Dec 2023
MATLAB AI Chat Playground solved this problem for me. Following is its answer.
MATLAB does not have a built-in way to determine whether it is running in the background or in the foreground. However, you can use the desktop function to check whether the MATLAB desktop is running. If the desktop is running, then MATLAB is likely running in the foreground. If the desktop is not running, then MATLAB is likely running in the background.
if desktop('-inuse')
disp('MATLAB is running in the foreground.')
else
disp('MATLAB is running in the background.')
end
Note that this method may not work in all cases, as the `desktop` function may not be available in some MATLAB configurations.

More Answers (1)

Pratyush
Pratyush on 24 Nov 2023
Hi Lala,
Since Visual Studio Code extension does not support execution or debugging of MATLAB code (refer to this MATLAB Blog to know the limitations of the extension: https://blogs.mathworks.com/matlab/2023/04/26/do-you-use-visual-studio-code-matlab-is-now-there-too/#:~:text=What%E2%80%99s%20missing%20from%20this%20version%20of%20the%20extension%3F), I understand that different starting mode means, you want to distinguish if the MATLAB program is running in a MATLAB session mode (i.e. in the MATLAB IDE) or as a stand alone program (i.e. compiled using MATLAB compiler).
You can use the function "isdeployed" which returns logical 1 (true) when the function is running in deployed mode using MATLAB® Runtime and 0 (false) if it is running in a MATLAB session. You can refer to the following documentation for more details: https://in.mathworks.com/help/compiler/isdeployed.html
  1 Comment
lala
lala on 24 Nov 2023
Thanks for your reply but the function "isdeployed" is not working. It return logical 0 (false) in startup.m script both in the MATLAB IDE and vscode.
You can try following script and name it with startup.m, put it in the search path.
if ~isdeployed
answer = inputdlg("Enter a number");
else
answer = 10;
end
When you open MATLAB by double click from desktop, it will show you the dialog box. When you open vscode with MATLAB official extension, it will also show you the dialog box (see the figure below). So it seems that the "isdeployed" cannot distinguish the start mode.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!