How do I run a MatLab script via the windows command prompt such that it does not output back to the terminal when using the -batch argment?

16 views (last 30 days)
I am trying to run a matlab script via the windows command prompt using the -batch argument, but in a manner where it does not log text back to the terminal (stdout and stderr). I tried appending with >nul to discard the output, but matlab still logged to the output. I dont neccesarily need to use the -batch argument, but I cannot see another method to run a matlab script silently (except for the writing back to output issue) with the remaining R2022b list of optional arguments. Any solution for this would be greatly appreciated.
Thanks in advance.

Answers (1)

dpb
dpb on 20 Jan 2023
stderr is the second output stream and its redirection symbol syntax is "2>"
To redirect both to same place use
> nul 2>&1
The first directs stdout to nul; the second redirects stderr (>2) to io stream 1 which has been redirected to nul
I think one can also use the more verbose
> nul 2> nul
to accomplish the same thing in a more human-legible format.
  2 Comments
Walter Roberson
Walter Roberson on 20 Jan 2023
I do not know about redirection to nul in particular. If the output were being redirected to a file, then the 2>&1 form needs to be used or else Windows will complain the file is already in use. That might possibly not be the case for NUL:
dpb
dpb on 20 Jan 2023
Edited: dpb on 20 Jan 2023
Microsoft Windows [Version 10.0.19044.2486]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Duane>dir fred.tmp
Volume in drive C is Windows
Volume Serial Number is 68A4-CD84
Directory of C:\Users\Duane
File Not Found
C:\Users\Duane>dir fred.tmp >nul
File Not Found
C:\Users\Duane>dir fred.tmp >nul 2>nul
C:\Users\Duane>
Windows seems perfectly happy with the bit bucket redirection either way. Not terribly surprising with a physical file.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!