How does MATLAB know it ran from vscode or desktop?

11 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by