Reputation: 61
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?
Upvotes: 4
Views: 3479
Reputation: 60695
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.
Upvotes: 4
Reputation: 620
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.
Upvotes: 5
Reputation: 61
I have found a solution at:
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.
Upvotes: 2
Reputation: 5672
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:
-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.
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.
Upvotes: 1