简体   繁体   中英

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

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

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 xy plane.

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.

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

The "MATLAB Command Window" looks like below: 在此处输入图像描述

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:

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.)

Great news!

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

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 Engine to display your figure. Additionally, if it's for simple things like plotting, etc. you could use GNU Octave which 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).

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.

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

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

Or in 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.

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 pause call, so it doesn't disappear before they're done viewing it.

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');"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM