exportgraphics in MATLAB online only gives empty PNG-file.

15 次查看(过去 30 天)
Hi everybody,
I really cannot figure out what I am doing wrong here. When I run this code in a .m-file on MATLAB online (R2022a, Update 4), I only get an empty png file (0KB). It used to work well.. Also manually saving the file to the online folder gives a 0KB PNG-file.
Must be something about the gca, I guess.
t= [0 60 163 204 282 361 415 460 560 761];
U = [9.05 8.00 6.44 6.00 5.18 4.41 4.00 3.61 2.98 2.00];
plot(t,U);
%title and stuff
title('Grafic to export');
xlabel('X-factor');
ylabel('I don''t know Y ');
%export
ax = gca;
filename='ExportTest.png';
exportgraphics(ax,filename);
I appreciate your help and thanks a lot.
C.
EDIT: It works when I run exportgraphics on the command line. But why wont it export when it runs in the .m-file?

回答(1 个)

Richard Quist
Richard Quist 2022-8-26
Your original code looks like it should work - I suggest contacting support for help.
In the meantime, you could try the following 2 potential work arounds
Workaround #1: add a call to drawnow before the call to exportgraphics. If it's a timing issue where the plot wasn't drawn when exportgraphics started to process it then this may help.
% ...
% ^^^ your original code to create the plot ^^^
filename='ExportTest.png';
% NEW: call drawnow to give the plot a chance to render
drawnow;
% your export call
exportgraphics(ax,filename);
Workaround #2: Rather than use gca, try creating the axes separately and then using that axes handle when plotting your data, adding the title, etc, and in the call to exportgraphics.
ax = axes;
t= [0 60 163 204 282 361 415 460 560 761];
U = [9.05 8.00 6.44 6.00 5.18 4.41 4.00 3.61 2.98 2.00];
plot(ax, t,U); % <<= use ax here
%title and stuff
title(ax, 'Grafic to export'); % <<= use ax here
xlabel(ax, 'X-factor'); % <<= use ax here
ylabel(ax, 'I don''t know Y '); % <<= use ax here
%export
%ax = gca; % <<= Don't use gca here
filename='ExportTest.png';
exportgraphics(ax,filename);

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by