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

前端 未结 4 2022
情歌与酒
情歌与酒 2020-12-18 06:35

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

C:\\\\matlab.exe -nodisplay -nosplash -nodesktop -r \"run(\'C:\\

相关标签:
4条回答
  • 2020-12-18 06:43
    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');"
    
    0 讨论(0)
  • 2020-12-18 06:47

    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.

    0 讨论(0)
  • 2020-12-18 07:00

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

    0 讨论(0)
  • 2020-12-18 07:06

    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.

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

    0 讨论(0)
提交回复
热议问题