exportgraphics in MATLAB online only gives empty PNG-file.

22 views (last 30 days)
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?

Answers (1)

Richard Quist
Richard Quist on 26 Aug 2022
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);

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!