How to save a figure of a specific size with exportgraphics

54 visualizaciones (últimos 30 días)
Blue
Blue el 13 de Oct. de 2021
Comentada: Blue el 14 de Oct. de 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 comentario
Mario Malic
Mario Malic el 13 de Oct. de 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)

Iniciar sesión para comentar.

Respuesta aceptada

Dave B
Dave B el 13 de Oct. de 2021
Editada: Dave B el 13 de Oct. de 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 comentarios
Dave B
Dave B el 14 de Oct. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by