Error with fprintf in non-interactive Matlab session (startup option -batch)

조회 수: 7 (최근 30일)
Stefan H
Stefan H 2022년 2월 11일
답변: Samay Sagar 2024년 9월 25일
Hello everybody,
I just encountered an error when I open a new instance of Matlab with the startup option "-batch"
The error occurs when using fprintf within a for loop. Here's a code with the required lines to trigger the error:
cmdStrings = { ...
'diary on;' ...
'idcs = 1:100;' ...
'for f = 1:length( idcs );' ...
'fprintf(''>>> %d\n'', idcs(f) );' ...
'end;' ...
};
% compose commands as one string to hand it to new instance:
nStrings = length( cmdStrings );
sprintfStr = repmat( '%s', [1, nStrings] );
thisCmd = sprintf( sprintfStr, cmdStrings{:} );
% prepare command to invoke new instance with startup options:
cmndMatlab = sprintf( '!matlab -noFigureWindows -batch "%s" &', thisCmd );
eval( cmndMatlab ) ;
This will open a new instance of Matlab and create a file named "diary" in the default startup path.
The error can be seen in this diary file. Here's what is says:
>>> 1
>>> Error writing to output stream.
iostream stream error
{Error using fprintf
Error writing to output stream.
iostream stream error
}
Please note that the first iteration executes correctly and that the error happens not till the second iteration.
I have also done a try and catch approach to gain more details about the error:
E.identifier = 'iolib:badbit'
E.message = 'Error writing to output stream.iostream stream error'
This error only happens in batch-mode.
If I use the startup option "-r" instead (which opens an interactive session of Matlab), the code executes without any problems.
Also, if I convert the index into a string before handing it to fprintf I also don't get an error.
'thisNum = num2str( idcs(f) );' ...
'fprintf(''>>> %s\n'', thisNum );' ...
But this solution is not satisfying, because the script I'm trying to execute is very large and has a lot of subfunctions that use fprintf.
I'm using Matlab Version MATLAB Version: 9.7.0.1190202 (R2019b)
Any help would be very much appreciated.
Best regards,
Stefan
  댓글 수: 1
Erik Newton
Erik Newton 2024년 1월 19일
I've just hit this problem using R2021b.
I've had to go with using -r, and then adding ;exit on the end of my command.
E.g.
matlab -nodesktop -wait -r runTests;exit

댓글을 달려면 로그인하십시오.

답변 (1개)

Samay Sagar
Samay Sagar 2024년 9월 25일
I have also encountered this issue in the past. Batch mode requires using the MATLAB Starter EXE ("<matlabroot>/bin/matlab.exe") to redirect output to the terminal it was launched from. When running "!matlab" from the MATLAB prompt, the starter is skipped and "bin/<arch>/matlab.exe" is used instead which may not work as intended as it will not be connected to the output and error streams correctly.
You can modify the above script to use the starter EXE file instead of just "matlab":
matlabBin = [matlabroot(), '\bin\matlab'];
% prepare command to invoke new instance with startup options:
cmndMatlab = sprintf( '!%s -nodesktop -noFigureWindows -batch "%s" &',matlabBin, thisCmd );
eval( cmndMatlab ) ;
You can also use the "batch" function to run multiple scripts in background. You can refer the following documentation for more information about "batch" function:

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by