How to save a figure of a specific size with exportgraphics
54 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Mario Malic
le 13 Oct 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)
Réponse acceptée
Dave B
le 13 Oct 2021
Modifié(e) : Dave B
le 13 Oct 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 commentaires
Dave B
le 14 Oct 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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Printing and Saving dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!