How to save a figure of a specific size with exportgraphics

54 Ansichten (letzte 30 Tage)
Blue
Blue am 13 Okt. 2021
Kommentiert: Blue am 14 Okt. 2021
Hello,
I simply want to export a figure of a specific size (6 x 9 inches) with the function exportgraphics as described here (https://www.mathworks.com/help/matlab/creating_plots/save-figure-at-specific-size-and-resolution.html)
The following code doesnt return any errors but the figure is empty. Any tips ?
Thank you,
t = tiledlayout(1,1,'Padding','tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 6 9];
nexttile;
figure
subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
subplot(2,1,2);
y2 = sin(5*x);
plot(x,y2)
exportgraphics(t, 'test.jpg', 'Resolution', 300)
  1 Kommentar
Mario Malic
Mario Malic am 13 Okt. 2021
Hi,
You need to specify the parent figure to use the exportgraphics.
I am unable to figure out completely what's happening in the code.
This might do it.
fig = gcf;
exportgraphics(fig, 'test.jpg', 'Resolution', 300)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dave B
Dave B am 13 Okt. 2021
Bearbeitet: Dave B am 13 Okt. 2021
you created a tiledlayout in one figure, set some of its characteristics but didn't add anything to it. Then you created a new figure with subplots, then you exported the (empty) tiledlayout.
Instead, use tiledlayout to set your layout shape, drop the call to figure, and use nexttile in place of the calls to subplot:
t = tiledlayout(2, 1, 'Padding', 'tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 3 3];
nexttile;
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
nexttile;
y2 = sin(5*x);
plot(x,y2)
exportgraphics(t, 'test.jpg', 'Resolution', 300)
  3 Kommentare
Dave B
Dave B am 14 Okt. 2021
How about:
t = tiledlayout(2, 1, 'Padding', 'tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 6 6];
nexttile;
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
nexttile;
y2 = sin(5*x);
plot(x,y2)
% if you want your figure to go back to where it was after export, you can
% store the current units and position and set them back after exporting
set(gcf,'Units','inches','Position',[1 1 8 8]) % I used width and height of 8 to be sure nothing got cut off
exportgraphics(t, 'test.jpg', 'Resolution', 300)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by