windows 从命令行运行 m 文件时如何隐藏“MATLAB 命令行窗口”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6717688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:27:34  来源:igfitidea点击:

How do I hide "MATLAB Command Window" when I run an m-file from command line?

windowsmatlabuser-interfacecommand-linecommand-prompt

提问by hkBattousai

I am running MATLAB with a command line string like this:

我正在使用这样的命令行字符串运行 MATLAB:

C:\<a long path here>\matlab.exe -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');"

C:\<a long path here>\matlab.exe -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');"

The m-file contains a plot()function to plot a simple curve on the x-y plane.

m 文件包含plot()在 xy 平面上绘制简单曲线的函数。

The m-file successfully runs and draws the plotting with the command line string I specified above. However, every time I run this command, a window named "MATLAB Command Window" appears along with the plotting.

m 文件成功运行并使用我上面指定的命令行字符串绘制绘图。但是,每次运行此命令时,都会出现一个名为“MATLAB 命令窗口”的窗口以及绘图。

How do I make this "MATLAB Command Window" NOT appear, so that only the plotting will be visible.

如何使这个“MATLAB 命令窗口”不出现,以便只有绘图可见。

The "MATLAB Command Window" looks like below: enter image description here

“MATLAB 命令行窗口”如下所示: 在此处输入图片说明

回答by Jacob

Great news!

好消息!

With a bit of Java manipulation, it is possible! Start MATLAB normally(with the desktop etc.) Now run setDesktopVisibility(false) and voila! E.g.

通过一些 Java 操作,这是可能的!正常启动 MATLAB (使用桌面等)现在运行setDesktopVisibility(false) 和瞧!例如

setDesktopVisibility(false);
mesh(rand(10));
pause;
setDesktopVisibility(true);


AFAIK you can't do it on Windows using the options with matlab.exe. If you really need to hide it, I'd recommend using the MATLAB Engineto display your figure. Additionally, if it's for simple things like plotting, etc. you could use GNU Octavewhich works with M files and does not have a "Command Window" like MATLAB does (it runs in the Windows Command Prompt and hiding it is not that hard).

AFAIK 你不能在 Windows 上使用带有matlab.exe. 如果您确实需要隐藏它,我建议您使用MATLAB 引擎来显示您的图形。此外,如果是为了简单的事情,比如绘图等,你可以使用GNU Octave,它可以处理 M 文件并且没有像 MATLAB 那样的“命令窗口”(它在 Windows 命令提示符中运行,隐藏它并不难) .

回答by Andrew Janke

If you are a running Matlab from another program on Windows, you can run it using the Matlab COM Automation Server. The ActiveX control has a Visible property which will let you make the command window invisible, but looks like it leaves the plots visible.

如果您是从 Windows 上的另一个程序运行 Matlab,则可以使用Matlab COM 自动化服务器运行它。ActiveX 控件有一个 Visible 属性,它可以让你使命令窗口不可见,但看起来它使绘图可见。

Here's an example of how to do it using another Matlab as the controller.

这是一个如何使用另一个 Matlab 作为控制器的示例。

ml = actxserver('Matlab.Application');
ml.Visible = false;
ml.Execute('surf(peaks)');

Or in VBScript.

或者在 VBScript 中。

Set ml = CreateObject("Matlab.Application")
ml.Visible = false
ml.Execute("surf(peaks)")
ml.Execute("pause(4)")

This interaction mode might be more what you want anyway, depending on how your workflow is structured, because it'll let you fire up the Matlab process once and make many plot requests on it, saving startup costs and letting you have multiple plots visible at once.

无论如何,这种交互模式可能更符合您的要求,具体取决于您的工作流程的结构,因为它可以让您一次启动 Matlab 过程并在其上发出许多绘图请求,从而节省启动成本并让您可以在以下位置看到多个绘图一次。

If you still want to call it from a command line, just run it through a .vbs wrapper script with the above VBScript code, but call run('...\mfile.m')instead of surf(peaks). Your mfile.m will need some GUI logic that makes it block until the user dismisses the plot, replacing the pausecall, so it doesn't disappear before they're done viewing it.

如果您仍想从命令行调用它,只需使用上述 VBScript 代码通过 .vbs 包装脚本运行它,但调用run('...\mfile.m')而不是surf(peaks). 您的 mfile.m 将需要一些 GUI 逻辑使其阻塞,直到用户关闭绘图,替换pause调用,因此在他们完成查看之前它不会消失。

回答by Frank

Run:

跑:

matlab -automation -wait -r "cd \'...\';..."

,which will show a minimized Window in the user session. By suggestion from Amro, we can send the minimized window to winlogin session locally so that we even cannot see the minimized Window:

,这将在用户会话中显示一个最小化的窗口。根据 Amro 的建议,我们可以将最小化的窗口发送到本地的 winlogin 会话,这样我们甚至看不到最小化的窗口:

psexec /i 0 matlab -nodesktop -wait -r "plot(rand(100,1)); print -dpng out.png;quit" >null 2>&1

,which will save the figure to C:\Windows\System32 silently (if ISD service is enabled it may pop up an interactive services detection dialog window, and /s or /x option do not work in Windows server 2003 or 2008.)

,将图形静默保存到 C:\Windows\System32(如果启用了 ISD 服务,它可能会弹出一个交互式服务检测对话框窗口,并且 /s 或 /x 选项在 Windows server 2003 或 2008 中不起作用。)

回答by Ben Voigt

com.mathworks.mde.desk.MLDesktop.getInstance.closeCommandWindow

You can probably use it from the command-line as:

您可能可以从命令行使用它作为:

-r "com.mathworks.mde.desk.MLDesktop.getInstance.closeCommandWindow; run('C:\<a long path here>\mfile.m');"