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

18 次查看(过去 30 天)
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,8:53
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