How can I redirect the Command Window output to stdout and stderr when running MATLAB 7.8 (R2009a) in batch mode?

69 vues (au cours des 30 derniers jours)
My third party application has an ability similar to the DOS or SYSTEM functions in MATLAB: it can call an other application and capture the results. Through this ability I want to call MATLAB in batch mode, when I do this I want to be able to capture the output which is normally displayed in the Command Window. It appears however that MATLAB does not write this output to stdout and thus cannot be captured by my third party application. Is there another way to do this?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 9 Août 2012
The ability to automatically redirect the Command Window output to stdout or stderr is not available in MATLAB 7.8 (R2009a).
There are two possible workarounds.
First, you could write your own wrapper console application which makes use of MATLAB Engine to:
1. Execute commands (using "engEvalString")
2. Capture the output (using "engOutputBuffer")
3. Redirect the output to stdout or stderr (using "fprintf")
A sample application written in C is given below (this example can also be downloaded at the bottom of this page):
/* File: mlcaller.c */
#include <stdio.h>
#include "engine.h"
#define BUFFERSIZE 255
int main(int argc, char *argv[]) {
Engine *en;
int res;
char buffer[BUFFERSIZE];
/* Open connection to MATLAB */
en = engOpen(NULL);
/* Define that Command Window output should be written to buffer */
engOutputBuffer(en, buffer, BUFFERSIZE);
/* Pass the input argument to MATLAB */
res = engEvalString(en, argv[1]);
/* Check if an error occured. Error messages usually start with "???" */
if (buffer[0]!='?') {
fprintf(stdout,"%s\n",buffer);
} else {
fprintf(stderr,"%s\n",buffer);
}
/* Close the engine */
engClose(en);
}
This example can be compiled in MATLAB using:
mex('-f', [matlabroot '\bin\' computer('arch') '\mexopts\lccengmatopts.bat'], 'mlcaller.c');
Once compiled you can use this application as follows:
mlcaller "myMatlabFunction; myOtherFunction;"
Second, If MATLAB is being launched from the command line to execute a script in batch mode, then there is an indirect solution that may be simpler. For this example, we will assume that the script you want to run inside MATLAB is called "myscript.m".
Step 1: Produce a file containing all output to the command window. One way to do this is to create a wrapper script in MATLAB using the DIARY function, and save it (for example) as 'displaymyscript.m':
% Turn on recording of the command window output
diary('outfile.txt');
% Execute your script
myscript;
% Turn recording back off
diary off;
Step 2: Create a windows batch file to send the contents of the file you just created to the screen. The example batch file below accomplishes this using the TYPE command
@echo off
matlab -r displaymyscript -nosplash -nodesktop -wait
type outfile.txt
Executing the batch file rather than launching MATLAB directly from the command line will result in the command window output being displayed at the OS command prompt. Since this approach first creates a text file containing MATLAB command line output, an output is only displayed once the MATLAB script finishes execution, and the batch script reaches the TYPE command to display it on the console.
  2 commentaires
Antoine Pichot
Antoine Pichot le 6 Nov 2015
The ability to automatically redirect the Command Window output to stdout or stderr is not available in MATLAB 7.8 (R2009a).
Is there a more recent version of matlab that supports this?
Abhishek Pandey
Abhishek Pandey le 18 Avr 2016
Hi Antoine,
I believe that this has still not been implemented in MATLAB. If the workaround provided above does not work for you, please contact MathWorks Technical Support and raise a help ticket. I am sure our team will be very happy to help you.
- Abhishek

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Produits


Version

R2009a

Community Treasure Hunt

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

Start Hunting!

Translated by