Azzera filtri
Azzera filtri

How to save a figure of a specific size with exportgraphics

46 visualizzazioni (ultimi 30 giorni)
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 Commento
Mario Malic
Mario Malic il 13 Ott 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)

Accedi per commentare.

Risposta accettata

Dave B
Dave B il 13 Ott 2021
Modificato: Dave B il 13 Ott 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 Commenti
Dave B
Dave B il 14 Ott 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)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Printing and Saving in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by