4

I am running a MATLAB script from the Windows command prompt:

"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"

The test.m is simple:

function test
disp('Hello!');

The output is displayed in the Matlab Command Window. Is there any way how I can force output to the windows prompt?

2
  • This isn't quite the answer your looking for but you could use the system command to invoke echo but yea that would require you to modify your m files. Commented Nov 30, 2016 at 2:43
  • @Danny. Thank you for the comment. Yes, it won't help, since the output of the system will also go to the command window, instead of windows command prompt. Commented Nov 30, 2016 at 17:32

4 Answers 4

5

Use the command line option -log when you call Matlab from the command line (or any other shell or batch (e.g. cmd or bat) script).
It isn't documented as of Matlab 2017b, but it works.

Side note: -nodisplay isn't supported in the Windows version of Matlab, but if you want to prevent it from displaying figures, use -noFigureWindows instead.

Sign up to request clarification or add additional context in comments.

1 Comment

I need both -log and -wait for output to show up in console.
4

Since R2019b, there is a new command line option, -batch, which redirects the output to the command line and handles other stuff for batch processing. See the documentation for Windows.

matlab -batch "statement to run"

This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.

Comments

2

I have found a solution at:

https://www.mathworks.com/matlabcentral/answers/91607-how-can-i-redirect-the-command-window-output-to-stdout-and-stderr-when-running-matlab-7-8-r2009a-i#answer_100958

I will replicate it here for convenience. First I need to modify the matlab script to output to a text file:

function test
fid=fopen('output.txt','w');
fprintf(fid,'Hello!');
fclose(fid);

Then I should run the Matlab using a bat file with one additional command to display the contents of the output.txt:

"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
type output.txt

The type command will display the contents of 'output.txt' in the command window. So answer from @matlabgui was almost there. Thank you.

It is not a very elegant solution, but it works.

Comments

1

I don't know of a way to do it in Windows to get Matlab to actually run in the DOS window which is what you would need for the display to be written in it. (FYI: You can in LINUX - but I assume you need to run in Windows).

For running in Matlab you have 2 alternatives that I can think of:

  1. -logfile FILE on launch which will record all output to a FILE you specify - however how and when the file is written to disk is controlled by Matlab and I haven't tested to see - if your code doesn't do much it might only be written on Matlab exit.

  2. diary FILE in your Matlab command, i.e. -r "diary FILE.TXT; test.m; diary OFF" - this is similar to above - but uses the diary function.

However you can get what you want if you can run your code compiled (I know thats a big if as you may not have the compiler - or if you regularly want to update test.m this is not the most efficient...

When you run a compiled code from a DOS prompt all the terminal messages are written to the DOS prompt. One thing I'd advise if this is suitable is to delete the "splash.png" file from your installation directory to avoid the splash screen displaying when you run from the DOS as its (probably) not needed.

3 Comments

Hi matlabgui. Thanks for the answer. I've tried the compilation option. For some reason it works much slower compared to the batch Matlab execution. And, yes, I need to update the script pretty often.
To @matlabgui. And there is a problem with the diary and output options. I actually need to run the script from within another program, which waits for a certain output from the script to proceed. It just won't check the diary files. Anyway, I appreciate your help.
Make your Matlab script save a file with the desired output and watch for that file in your other program. That solution will be much easier. The compiled version Will be slower due to the time it takes to load the MCR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.