필터 지우기
필터 지우기

How does MATLAB know it ran from vscode or desktop?

조회 수: 12 (최근 30일)
lala
lala 2023년 11월 24일
편집: lala 2023년 12월 13일
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?

채택된 답변

lala
lala 2023년 12월 13일
편집: lala 2023년 12월 13일
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.

추가 답변 (1개)

Pratyush
Pratyush 2023년 11월 24일
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
lala
lala 2023년 11월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by