Catch command line information when using exportgraphics

7 visualizaciones (últimos 30 días)
Cunxin
Cunxin el 28 de Nov. de 2024
Respondida: Cunxin el 29 de Nov. de 2024
Hi there,
I met a problem when using MATLAB function "exportgraphics".
I open MATLAB under "nodesktop" mode and use "exportgraphics" to save a PDF file. However, in comand line, following information appears:
<</ID[<DEAD1C2145F3D2B9769CD2727BE248DF><DEAD1C2145F3D2B9769CD2727BE248DF>]/Info 4 0 R/Root 1 0 R/Size 448>>
<</ID[<8905FEFDDA7FE7FA1620E6FD378F0C4E><8905FEFDDA7FE7FA1620E6FD378F0C4E>]/Info 1 0 R/Root 45 0 R/Size 46>>
Fixing references in 448 0 R by 447
Fixing references in 449 0 R by 447
Fixing references in 450 0 R by 447
Fixing references in 451 0 R by 447
I tried to use "evalc" to omit these message but failed. It seems that these information are printed by lower-level APIs so "evalc" cannot help.
Is there any way to catch all the messages that "exportgraphics" prints? Thanks.

Respuesta aceptada

Cunxin
Cunxin el 29 de Nov. de 2024
"
This issue was fixed in R2024a.
The problem occurred when setting append to true in exportgraphics.
"

Más respuestas (1)

Ruchika Parag
Ruchika Parag el 28 de Nov. de 2024
Hi @Cunxin Huang, when using MATLAB in "nodesktop" mode, certain functions might produce output directly to the command line that can be difficult to suppress, especially if they originate from lower-level APIs. The 'exportgraphics' function, used for exporting figures to files, might be triggering such output if it interacts with the underlying graphics or file system libraries.
Here are a few ways you can try to suppress or redirect these messages:
1. Suppress Command Window Output
Although 'evalc' is typically used to capture output, it may not work if the messages come from lower-level system calls. Instead, you can try redirecting or suppressing command-line output using the following methods:
Redirect Output to a File
You can redirect the standard output to a file temporarily, which might help capture or suppress the messages:
% Open a temporary file to redirect output
tempFile = 'tempOutput.txt';
fid = fopen(tempFile, 'w');
% Save the current state of the diary
diaryState = get(0, 'Diary');
% Start redirecting output
diary(tempFile);
% Your code to export graphics
exportgraphics(gca, 'output.pdf');
% Stop redirecting
diary off;
% Restore the diary state
if strcmp(diaryState, 'on')
diary on;
end
% Optionally read the file to see the output
outputContent = fileread(tempFile);
% Close and delete the temporary file
fclose(fid);
delete(tempFile);
2. Use MATLAB's 'system' Command
If the messages are printed by external processes, you could try running MATLAB commands using the 'system' function to execute MATLAB in a subprocess, which might help isolate the output:
% Construct a MATLAB command to run in a separate process
matlabCommand = 'matlab -batch "exportgraphics(gca, ''output.pdf'')"';
% Run the command using the system function
[status, cmdout] = system(matlabCommand);
% Check the status and output
if status == 0
disp('Graphics exported successfully.');
else
disp('An error occurred:');
disp(cmdout);
end
These methods should help you manage or suppress unexpected output from the 'exportgraphics' function.
  1 comentario
Cunxin
Cunxin el 29 de Nov. de 2024
Thank you very much for your answer. I tried the first way you provided but it does not work. The second way sounds promising but I don't want to make my code strange.
I find another one has already asked the similar question, and the answer is that it is a bug by exportgraphics function and MATLAB 2024b has solved this problem. See links https://ww2.mathworks.cn/matlabcentral/answers/2148229-exportgraphics-causing-strange-messages-in-terminal-only-for-compiled-version-of-app
Anyway, thanks again for the kind comment.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by